E9.2 Groovy Script Returning Formatted Json Help

dtujo2022

dtujo2022

Member
So I have gotten the hang of create custom formatted json within a Groovy script, but what I am struggling with is returning the Json in the orchestration as the object and not a string.
Here is what I have:
Java:
import com.oracle.e1.common.OrchestrationAttributes;
import java.text.SimpleDateFormat;
import groovy.json.*

HashMap<String, Object> main(OrchestrationAttributes orchAttr, HashMap inputMap)
{
  HashMap<String, Object> returnMap = new HashMap<String, Object>();
 
    def inputData = (String)inputMap.get("serialData")
    def jsonSlurper = new JsonSlurper();
    def datajson = jsonSlurper.parseText(inputData);
    def workOrders = datajson.fs_DATABROWSE_V553112N.data.gridData.rowset;
 
    def output = [
        "LoadNumber": (String) inputMap.get("LoadNumber"),
        "WorkOrders": []
    ]
 
    def workOrderMap = [:]

    workOrders.each { row ->
        def woKey = row.F553112L_DOCO
        def opKey = row.F5548S1_OPSQ

        if(!workOrderMap.containsKey(woKey)) {
            workOrderMap[woKey] = [
                "WONumber": row.F553112L_DOCO,
                "WOType": row.F553112L_DCTO,
                "WOOperations": []
            ]
        }

        def workOrder = workOrderMap[woKey]

        def operation = workOrder.WOOperations.find {it.OperationSequence == opKey}
        if(!operation){
            operation = [
                "OperationSequence": row.F5548S1_OPSQ,
                "OperationCode": row.F553112L_Y55OP,
                "Serials": []
            ]
            workOrder.WOOperations << operation
        }

        operation.Serials << ["SerialNumber": row.F5548S1_LOTN]
    }

    output.WorkOrders = workOrderMap.values().toList()
    
  returnMap.put("jsonout", output);

  return returnMap;
}
1715981016670.png
1715981034038.png
This is what it returns:
1715981061788.png


UPDATE- I had to add "def jsonOutput = JsonOutput.toJson(output)"
This got it working.
 
Last edited:
I "tested" myself here and didn't look this up in ChatGPT (which will come as a shocking surprise to regulars here)

The issue is, you've done GREAT to get your data into the format you want. jsonSlurper is hugely powerful, but it doesn't handle formatting of the data properly if you are after json output. To get the final mile, you need to use jsonBuilder to format your jsonSlurper object.

I think you're trying to figure this out "under your own power" so I'll leave it here. Good luck and report back!

Finally I would really encourage you to do what I didn't do for over a year, and take the plunge into Jython for your JSON work. It's much much much simpler and more approachable than groovy.
 
Ooh I see you solved it yourself! I didn't know you didn't need jsonBuilder.
 
I "tested" myself here and didn't look this up in ChatGPT (which will come as a shocking surprise to regulars here)

The issue is, you've done GREAT to get your data into the format you want. jsonSlurper is hugely powerful, but it doesn't handle formatting of the data properly if you are after json output. To get the final mile, you need to use jsonBuilder to format your jsonSlurper object.

I think you're trying to figure this out "under your own power" so I'll leave it here. Good luck and report back!

Finally I would really encourage you to do what I didn't do for over a year, and take the plunge into Jython for your JSON work. It's much much much simpler and more approachable than groovy.

Is Jython available by default at some TR level or do you need to enable it? I think I would prefer Jython (or Ruby) to Groovy but currently only option in our main environment is Groovy.

Edit 1:
May have answered my own question. I found a doc on Oracle about how to enable it and:

"Download either Jython 2.7.2 (tested with AIS 9.2.5.4 through 9.2.7.3) or Jython 2.7.3 (tested with AIS 9.2.7.4 and later)."

We are on TR9.2.4.3 and TR9.2.5.3 so I am guessing if it does work, its not supported at our TR level????
 
Last edited:
I will rely on my smarter & actual-experienced counterparts for the answers to your question! But I do know that at some point in the TR's, groovy is no longer preinstalled, and Jython & Jruby ARE (?). keeping groovy going involves a special install from your cnc. If your cnc really loves you, you will get access to all 3 languages ongoing.
 
Out of curiosity, what does the Orchestrator UI look like when you enable other scripting languages? In other words when I expand the "Manipulate Output" on my TR it simply has a text editor box captioned "Groovy".
 
Despite groovy not being installed by default I always have my clients install it. I have used it from the beginning and still cling to it. It works great. I don't consider myself to be an expert and don't always know the best way to do things, sometimes it takes me a while to get things done because I have to research how best to do the things I need with the language. The biggest thing for me was reading json strings and creating jason strings.

6-8 months ago I started to use ChatGPT to help me build code for my groovy script custom service requests. It has been a game changer. I give it the sample code given to us with each new groovy script object. I give it sample json string formats I need to parse or create. I give it instructions on exactly what I need it to do. It produces the code I need. After the initial response I go through some iterations and refinement with ChatGPT and done!

Some things I know would have taken me a day or 2 due to my lack of experience with groovy is done in a couple of hours. In some cases the code is correct on the first try. I always start with ChatGPT when creating new groovy script. You gotta try it.
 
Back
Top