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.