Flip7607
Flip7607

Reputation: 59

How to remove brackets from JSON?

I have a groovy script that I want to return userDefinedErrorText. The issue I am having is that when parse my JSON I am having my failedForm variable equal [Failed] instead of "Failed".

If I remove the first pair of [] from my JSON input, I get the correct "Failed".

Is there a way to remove [] from the input JSON?

My Groovy

def json = new JsonSlurper().parseText( aInputJson )

failedForm = json?.userDefinedErrorText
    
    if( failedForm == "Failed" ) {
        resultMessage = "false"
    }

JSON

[
  {
    "step": "abcd",
    "message": {
      "ServiceRequest: abc": {
        "App Stack Form Exception": {
          "Expecting Form": "P",
          "Resulting Form": "P"
        },
        "JAS Response": {
          "fs_P": {
            "title": "I",
            "data": {},
            "errors": [
              {
                "CODE": "799L",
                "TITLE": "Error: Invalid Long Address Number",
                "ERRORCONTROL": "15",
                "DESC": "CAUSE . . . . The long address number entered is not found in the Address Book\\u000a               Master file (F0101).\\u000aRESOLUTION. .  Enter a valid long address number.",
                "MOBILE": "The long address number entered is not found in the Address Book\\u000a               Master file (F0101)."
              }
            ],
            "warnings": []
          },
          "stackId": 12,
          "stateId": 5,
          "rid": "8f4",
          "currentApp": "P",
          "timeStamp": "2022-04-22:11.25.03",
          "sysErrors": []
        }
      }
    },
    "timeStamp": "2022-04-22T11:25:03.235-0400",
    "userDefinedErrorText": "Failed"
  }
]

Upvotes: 0

Views: 945

Answers (1)

Jeff Scott Brown
Jeff Scott Brown

Reputation: 27220

The issue I am having is that when parse my JSON I am having my failedForm variable equal [Failed] instead of "Failed".

The following should work:

String jsonString = '''
[
  {
    "step": "abcd",
    "message": {
      "ServiceRequest: abc": {
        "App Stack Form Exception": {
          "Expecting Form": "P",
          "Resulting Form": "P"
        },
        "JAS Response": {
          "fs_P": {
            "title": "I",
            "data": {},
            "errors": [
              {
                "CODE": "799L",
                "TITLE": "Error: Invalid Long Address Number",
                "ERRORCONTROL": "15",
                "DESC": "CAUSE . . . . The long address number entered is not found in the Address Book\\\\u000a               Master file (F0101).\\\\u000aRESOLUTION. .  Enter a valid long address number.",
                "MOBILE": "The long address number entered is not found in the Address Book\\\\u000a               Master file (F0101)."
              }
            ],
            "warnings": []
          },
          "stackId": 12,
          "stateId": 5,
          "rid": "8f4",
          "currentApp": "P",
          "timeStamp": "2022-04-22:11.25.03",
          "sysErrors": []
        }
      }
    },
    "timeStamp": "2022-04-22T11:25:03.235-0400",
    "userDefinedErrorText": "Failed"
  }
]'''
    

def json = new JsonSlurper().parseText(jsonString)
String value = json[0].userDefinedErrorText

assert value == 'Failed'

Upvotes: 2

Related Questions