E9.2 Manipulate output with groovy

ybuffetaud

ybuffetaud

Active Member
Hi I've made an orchestration to call an external rest (it is an excution of an semarchy XDI delivery).
This is working as expected : if I foget an imput parameter I have an error which is fine.
But I find the ouput ugly in this casr, this is like that :

{
"message": {
"ServiceRequest: CONN2_XDI_TEST_REST": {
"statusCode": 400,
"error": "java.lang.Exception: External Generic Call Failed See Log For Details: 400 {\"Code\":\"400\", \"Message\":\"Un ou plusieurs parametre est manquant en entr�e\"}",
"headers": {}
}
},
"exception": "Exception",
"timeStamp": "2024-01-10T16:22:59.323+0100",
"userDefinedErrorText": "",
"status": "ERROR",
"exceptionId": "b94b0e5e-22df-4c4a-85d1-2cf0f5b104c5"
}

I want to only see the following :
"statusCode": "400"
"Message" : "Un ou plusieurs parametre est manquant en entr�e"

I'm not a java nor a groovy developer, so how to do that?

Regards
 
Hi ybuffetaud
Try to put this code in output manipulation, first ask is there is an error and later replace the jsonIn with the especific error
if (jsonIn.status == "ERROR") {
def serviceRequest = jsonIn.message."ServiceRequest: CONN2_XDI_TEST_REST"
def result = [
statusCode: serviceRequest.statusCode.toString(),
Message: serviceRequest.error.replaceAll(/.*"Message":"([^"]*)".*/, '$1')
]

jsonIn =result
}
 
Hello! We are in a "golden age" of groovy because we no longer need to be experts, or go search Google. ChatGPT4 is very good at building groovy for what we need! (I really should do a conference presentation on this). It is important to read the resultant script and make effort to understand it so we can be sure there is no malicious code so that the robots can't take over the earth yet!

I fed your script more or less into chatGPT4, with a few extra bits here and there:

Prompt:
Code:
I have a JSON input file as such:
{
"message": {
"ServiceRequest: CONN2_XDI_TEST_REST": {
"statusCode": 400,
"error": "java.lang.Exception: External Generic Call Failed See Log For Details: 400 {\"Code\":\"400\", \"Message\":\"Un ou plusieurs parametre est manquant en entr�e\"}",
"headers": {}
}
},
"exception": "Exception",
"timeStamp": "2024-01-10T16:22:59.323+0100",
"userDefinedErrorText": "",
"status": "ERROR",
"exceptionId": "b94b0e5e-22df-4c4a-85d1-2cf0f5b104c5"
}

I want to use Groovy Script to only see the following:
{
"statusCode": "400",
"Message" : "Un ou plusieurs parametre est manquant en entr�e"
}

ChatGPT response:
Code:
To extract the specific information you need from the JSON input using Groovy, you can parse the JSON string and then access the desired fields. Here's a basic script to achieve this:

1. Parse the JSON string to a Groovy/Java object.
2. Navigate through the object to extract the `statusCode` and the `Message`.

Here's an example Groovy script:

```groovy
import groovy.json.JsonSlurper

def jsonString = '''{
    "message": {
        "ServiceRequest: CONN2_XDI_TEST_REST": {
            "statusCode": 400,
            "error": "java.lang.Exception: External Generic Call Failed See Log For Details: 400 {\"Code\":\"400\", \"Message\":\"Un ou plusieurs parametre est manquant en entr\u00E9e\"}",
            "headers": {}
        }
    },
    "exception": "Exception",
    "timeStamp": "2024-01-10T16:22:59.323+0100",
    "userDefinedErrorText": "",
    "status": "ERROR",
    "exceptionId": "b94b0e5e-22df-4c4a-85d1-2cf0f5b104c5"
}'''

def jsonSlurper = new JsonSlurper()
def jsonObject = jsonSlurper.parseText(jsonString)

def serviceRequest = jsonObject.message.'ServiceRequest: CONN2_XDI_TEST_REST'
def statusCode = serviceRequest.statusCode
def errorMessage = serviceRequest.error

// Extracting the message from the error string
def errorJsonString = errorMessage.split('400 ')[1]
def errorJsonObject = jsonSlurper.parseText(errorJsonString)
def message = errorJsonObject.Message

def result = [
    "statusCode": statusCode.toString(),
    "Message": message
]

println new groovy.json.JsonBuilder(result).toPrettyString()
```

This script does the following:
- Parses the JSON input.
- Extracts the `statusCode` and the `error` message from the `ServiceRequest: CONN2_XDI_TEST_REST` object.
- Splits the `error` string to get the JSON part and then parses it.
- Extracts the `Message` from the error JSON.
- Constructs a new JSON object with the `statusCode` and `Message`.
- Prints the result in a pretty JSON format.

You might need to adjust the script depending on the exact structure of your JSON and the Groovy environment you're working in.

I then modified the result to work within the orchestration groovy template, and rather than output raw json I assumed you might want the result in simple output variables:
Code:
import com.oracle.e1.common.OrchestrationAttributes;
import java.text.SimpleDateFormat;
import groovy.json.JsonSlurper;

HashMap<String, Object> main(OrchestrationAttributes orchAttr, HashMap inputMap)
{
    HashMap<String, Object> returnMap = new HashMap<String, Object>();
    // Add logic here
    String stringVal = (String)inputMap.get("inputStringVal");
    def jsonSlurper = new JsonSlurper()
    def jsonObject = jsonSlurper.parseText(stringVal)

    def serviceRequest = jsonObject.message.'ServiceRequest: CONN2_XDI_TEST_REST'
    def statusCode = serviceRequest.statusCode
    def errorMessage = serviceRequest.error

    // Extracting the message from the error string
    def errorJsonString = errorMessage.split('400 ')[1]
    def errorJsonObject = jsonSlurper.parseText(errorJsonString)
    def message = errorJsonObject.Message

    returnMap.put("OutStatusCode", statusCode.toString());
    returnMap.put("OutMessage", message);

    return returnMap;
}

This works great, as shown:
1705072031359.png

This solves for this specific output! Notice that we're doing old-school "visual basic scripting" style string splits on static expected text. So you might find if you need to handle other return messages that this falls apart, and you might need a more robust script down the line.

This took ~5 minutes to go from chatGPT prompt to working script! Life changing efficiency.
 
Hello! We are in a "golden age" of groovy because we no longer need to be experts, or go search Google. ChatGPT4 is very good at building groovy for what we need! (I really should do a conference presentation on this). It is important to read the resultant script and make effort to understand it so we can be sure there is no malicious code so that the robots can't take over the earth yet!

I fed your script more or less into chatGPT4, with a few extra bits here and there:

Prompt:
Code:
I have a JSON input file as such:
{
"message": {
"ServiceRequest: CONN2_XDI_TEST_REST": {
"statusCode": 400,
"error": "java.lang.Exception: External Generic Call Failed See Log For Details: 400 {\"Code\":\"400\", \"Message\":\"Un ou plusieurs parametre est manquant en entr�e\"}",
"headers": {}
}
},
"exception": "Exception",
"timeStamp": "2024-01-10T16:22:59.323+0100",
"userDefinedErrorText": "",
"status": "ERROR",
"exceptionId": "b94b0e5e-22df-4c4a-85d1-2cf0f5b104c5"
}

I want to use Groovy Script to only see the following:
{
"statusCode": "400",
"Message" : "Un ou plusieurs parametre est manquant en entr�e"
}

ChatGPT response:
Code:
To extract the specific information you need from the JSON input using Groovy, you can parse the JSON string and then access the desired fields. Here's a basic script to achieve this:

1. Parse the JSON string to a Groovy/Java object.
2. Navigate through the object to extract the `statusCode` and the `Message`.

Here's an example Groovy script:

```groovy
import groovy.json.JsonSlurper

def jsonString = '''{
    "message": {
        "ServiceRequest: CONN2_XDI_TEST_REST": {
            "statusCode": 400,
            "error": "java.lang.Exception: External Generic Call Failed See Log For Details: 400 {\"Code\":\"400\", \"Message\":\"Un ou plusieurs parametre est manquant en entr\u00E9e\"}",
            "headers": {}
        }
    },
    "exception": "Exception",
    "timeStamp": "2024-01-10T16:22:59.323+0100",
    "userDefinedErrorText": "",
    "status": "ERROR",
    "exceptionId": "b94b0e5e-22df-4c4a-85d1-2cf0f5b104c5"
}'''

def jsonSlurper = new JsonSlurper()
def jsonObject = jsonSlurper.parseText(jsonString)

def serviceRequest = jsonObject.message.'ServiceRequest: CONN2_XDI_TEST_REST'
def statusCode = serviceRequest.statusCode
def errorMessage = serviceRequest.error

// Extracting the message from the error string
def errorJsonString = errorMessage.split('400 ')[1]
def errorJsonObject = jsonSlurper.parseText(errorJsonString)
def message = errorJsonObject.Message

def result = [
    "statusCode": statusCode.toString(),
    "Message": message
]

println new groovy.json.JsonBuilder(result).toPrettyString()
```

This script does the following:
- Parses the JSON input.
- Extracts the `statusCode` and the `error` message from the `ServiceRequest: CONN2_XDI_TEST_REST` object.
- Splits the `error` string to get the JSON part and then parses it.
- Extracts the `Message` from the error JSON.
- Constructs a new JSON object with the `statusCode` and `Message`.
- Prints the result in a pretty JSON format.

You might need to adjust the script depending on the exact structure of your JSON and the Groovy environment you're working in.

I then modified the result to work within the orchestration groovy template, and rather than output raw json I assumed you might want the result in simple output variables:
Code:
import com.oracle.e1.common.OrchestrationAttributes;
import java.text.SimpleDateFormat;
import groovy.json.JsonSlurper;

HashMap<String, Object> main(OrchestrationAttributes orchAttr, HashMap inputMap)
{
    HashMap<String, Object> returnMap = new HashMap<String, Object>();
    // Add logic here
    String stringVal = (String)inputMap.get("inputStringVal");
    def jsonSlurper = new JsonSlurper()
    def jsonObject = jsonSlurper.parseText(stringVal)

    def serviceRequest = jsonObject.message.'ServiceRequest: CONN2_XDI_TEST_REST'
    def statusCode = serviceRequest.statusCode
    def errorMessage = serviceRequest.error

    // Extracting the message from the error string
    def errorJsonString = errorMessage.split('400 ')[1]
    def errorJsonObject = jsonSlurper.parseText(errorJsonString)
    def message = errorJsonObject.Message

    returnMap.put("OutStatusCode", statusCode.toString());
    returnMap.put("OutMessage", message);

    return returnMap;
}

This works great, as shown:
View attachment 20009

This solves for this specific output! Notice that we're doing old-school "visual basic scripting" style string splits on static expected text. So you might find if you need to handle other return messages that this falls apart, and you might need a more robust script down the line.

This took ~5 minutes to go from chatGPT prompt to working script! Life changing efficiency.
Thanks I added your step :
1705078208779.png
Now If I felle alla the parameter without forgetting one i Have using P98ORCH:
1705078311965.png
1705078346416.png
But If I forgot one parameter (my problem indeed), I stille have an issue :
1705078426583.png
1705078459638.png
I want in both cases the 2 Code and Message parameters
 
I issued a case in MyOracleSupport, because the custom groovy works alone but under my first step I have the following message :
{
"message": {
"SREQ_CLEAN_PAYLOAD_XDI_SAGE_FRP_IN_IN10_PAIE": {
"Exception": "IllegalArgumentException",
"Message": "Text must not be null or empty"
}
},
"exception": "Exception",
"timeStamp": "2024-01-17T14:19:35.923+0100",
"userDefinedErrorText": "",
"status": "ERROR",
"exceptionId": "83b7c8d4-d3e5-498c-8455-be18a9fd4eca"
}
 
Hi, if there's a chance for your script to NOT have input then you need to handle that with nulls. I asked chatGPT to modify the script to handle that:

 
I can't believe my employer has blocked access to chatGPT

Paranoid much :ROFLMAO:
 
I can't believe my employer has blocked access to chatGPT

Paranoid much :ROFLMAO:
wow. I could see it, if there would be a chance for careless use - uploading proprietary info that might show up in a future response outside the company. It's not quite like google in that ostensibly we're training it and providing it with context as we go along, so we need to be careful. wonder if they'd allow for case-by-case exemptions if you can prove you understand how to use it without breaching proprietary info?
 
Hello! We are in a "golden age" of groovy because we no longer need to be experts, or go search Google. ChatGPT4 is very good at building groovy for what we need! (I really should do a conference presentation on this). It is important to read the resultant script and make effort to understand it so we can be sure there is no malicious code so that the robots can't take over the earth yet!

I fed your script more or less into chatGPT4, with a few extra bits here and there:

Prompt:
Code:
I have a JSON input file as such:
{
"message": {
"ServiceRequest: CONN2_XDI_TEST_REST": {
"statusCode": 400,
"error": "java.lang.Exception: External Generic Call Failed See Log For Details: 400 {\"Code\":\"400\", \"Message\":\"Un ou plusieurs parametre est manquant en entr�e\"}",
"headers": {}
}
},
"exception": "Exception",
"timeStamp": "2024-01-10T16:22:59.323+0100",
"userDefinedErrorText": "",
"status": "ERROR",
"exceptionId": "b94b0e5e-22df-4c4a-85d1-2cf0f5b104c5"
}

I want to use Groovy Script to only see the following:
{
"statusCode": "400",
"Message" : "Un ou plusieurs parametre est manquant en entr�e"
}

ChatGPT response:
Code:
To extract the specific information you need from the JSON input using Groovy, you can parse the JSON string and then access the desired fields. Here's a basic script to achieve this:

1. Parse the JSON string to a Groovy/Java object.
2. Navigate through the object to extract the `statusCode` and the `Message`.

Here's an example Groovy script:

```groovy
import groovy.json.JsonSlurper

def jsonString = '''{
    "message": {
        "ServiceRequest: CONN2_XDI_TEST_REST": {
            "statusCode": 400,
            "error": "java.lang.Exception: External Generic Call Failed See Log For Details: 400 {\"Code\":\"400\", \"Message\":\"Un ou plusieurs parametre est manquant en entr\u00E9e\"}",
            "headers": {}
        }
    },
    "exception": "Exception",
    "timeStamp": "2024-01-10T16:22:59.323+0100",
    "userDefinedErrorText": "",
    "status": "ERROR",
    "exceptionId": "b94b0e5e-22df-4c4a-85d1-2cf0f5b104c5"
}'''

def jsonSlurper = new JsonSlurper()
def jsonObject = jsonSlurper.parseText(jsonString)

def serviceRequest = jsonObject.message.'ServiceRequest: CONN2_XDI_TEST_REST'
def statusCode = serviceRequest.statusCode
def errorMessage = serviceRequest.error

// Extracting the message from the error string
def errorJsonString = errorMessage.split('400 ')[1]
def errorJsonObject = jsonSlurper.parseText(errorJsonString)
def message = errorJsonObject.Message

def result = [
    "statusCode": statusCode.toString(),
    "Message": message
]

println new groovy.json.JsonBuilder(result).toPrettyString()
```

This script does the following:
- Parses the JSON input.
- Extracts the `statusCode` and the `error` message from the `ServiceRequest: CONN2_XDI_TEST_REST` object.
- Splits the `error` string to get the JSON part and then parses it.
- Extracts the `Message` from the error JSON.
- Constructs a new JSON object with the `statusCode` and `Message`.
- Prints the result in a pretty JSON format.

You might need to adjust the script depending on the exact structure of your JSON and the Groovy environment you're working in.

I then modified the result to work within the orchestration groovy template, and rather than output raw json I assumed you might want the result in simple output variables:
Code:
import com.oracle.e1.common.OrchestrationAttributes;
import java.text.SimpleDateFormat;
import groovy.json.JsonSlurper;

HashMap<String, Object> main(OrchestrationAttributes orchAttr, HashMap inputMap)
{
    HashMap<String, Object> returnMap = new HashMap<String, Object>();
    // Add logic here
    String stringVal = (String)inputMap.get("inputStringVal");
    def jsonSlurper = new JsonSlurper()
    def jsonObject = jsonSlurper.parseText(stringVal)

    def serviceRequest = jsonObject.message.'ServiceRequest: CONN2_XDI_TEST_REST'
    def statusCode = serviceRequest.statusCode
    def errorMessage = serviceRequest.error

    // Extracting the message from the error string
    def errorJsonString = errorMessage.split('400 ')[1]
    def errorJsonObject = jsonSlurper.parseText(errorJsonString)
    def message = errorJsonObject.Message

    returnMap.put("OutStatusCode", statusCode.toString());
    returnMap.put("OutMessage", message);

    return returnMap;
}

This works great, as shown:
View attachment 20009

This solves for this specific output! Notice that we're doing old-school "visual basic scripting" style string splits on static expected text. So you might find if you need to handle other return messages that this falls apart, and you might need a more robust script down the line.

This took ~5 minutes to go from chatGPT prompt to working script! Life changing efficiency.
I did the same, use chatGPT and post a repply to help is a wonderfull tool for me
 
Back
Top