Search code examples
groovy

Groovy Script to convert String to Integer values in the Json


I have a json in which i need to convert the certain strings into integer value, that is remove the double quotes , Below is input json

{
  "DELVRY07": {
    "IDOC": {
      "@BEGIN": "1",
      "EDI_DC40": {
        "@SEGMENT": "1",  
      },
      "E1EDL20": {
        "@SEGMENT": "1"
        "E1EDL21": {
          "@SEGMENT": "1"
        },
        "E1ADRM1": [
          {
            "@SEGMENT": "1",
          }
        ],
        "E1EDT13": [
          {
            "@SEGMENT": "1",
            "QUALF": "006",
            "NTANF": "20230711",
            "ISDD": "20230711"
          },
          {
            "@SEGMENT": "1",
            "QUALF": "003",
            "NTANF": "20230711"
          }
        ]
}}}}

and i need to convert the NATANF & ISDD field values into integer values that is to remove quotes .My outout json needs to be as below

{
  "DELVRY07": {
    "IDOC": {
      "@BEGIN": "1",
      "EDI_DC40": {
        "@SEGMENT": "1",  
      },
      "E1EDL20": {
        "@SEGMENT": "1"
        "E1EDL21": {
          "@SEGMENT": "1"
        },
        "E1ADRM1": [
          {
            "@SEGMENT": "1",
          }
        ],
        "E1EDT13": [
          {
            "@SEGMENT": "1",
            "QUALF": "006",
            "NTANF": 20230711,
            "ISDD": 20230711
          },
          {
            "@SEGMENT": "1",
            "QUALF": "003",
            "NTANF": 20230711
          }
        ]
}}}}

I am using below script but it does not work

def jsonSlurper = new JsonSlurper()
    def list = jsonSlurper.parseText(body)
    list['NTANF'] = Integer.parseInt(list['NTANF'])
list['ISDD'] = Integer.parseInt(list['ISDD'])
    def jsonOP = JsonOutput.toJson(list)

Any help would be highly appreciated, thanks


Solution

  • You can simply remove the quotes around numeric values in the original json String:

    import groovy.json.*
        
    String body = '''\
    {
      "DELVRY07": {
        "IDOC": {
          "@BEGIN": "1",
          "EDI_DC40": {
            "@SEGMENT": "1",  
          },
          "E1EDL20": {
            "@SEGMENT": "1",
            "E1EDL21": {
              "@SEGMENT": "1"
            },
            "E1ADRM1": [
              {
                "@SEGMENT": "1",
              }
            ],
            "E1EDT13": [
              {
                "@SEGMENT": "1",
                "QUALF": "006",
                "NTANF": "20230711",
                "ISDD": "20230711"
              },
              {
                "@SEGMENT": "1",
                "QUALF": "003",
                "NTANF": "20230711"
              }
            ]
          }
        }
      }
    }'''
    
    body = body.replaceAll( /"([1-9]\d+)"/, '$1' )
    
    def json = new JsonSlurper().parseText body
    
    assert Integer == json.DELVRY07.IDOC.E1EDL20.E1EDT13[ 0 ].NTANF.getClass()
    assert String == json.DELVRY07.IDOC.E1EDL20.E1EDT13[ 0 ].QUALF.getClass()
    assert Integer == json.DELVRY07.IDOC.E1EDL20.E1EDT13[ 0 ].ISDD.getClass()