jsongroovyjsonobjectrequest

How to access and loop though nested Json in Groovy


I am trying to access and loop a nested Json in Groovy for testing purpose, the file saved in a file below is how the structure is

{
  "dunsNumber": 0,
  "branches": 25,
  "url": "www.nbch.com.com",
  "address": {
    "continentId": 5,
    "continentName": "South & Central America",
    "countryId": 20,
    "countryName": "Brasil"
  },
  "parentOrganizations": [
    {
      "parentType": "IMMEDIATE",
      "name": "abcde",
      "dunsNumber": "12345",
      "address": {
        "continentId": 5,
        "continentName": "Europe",
        "countryId": 33,
        "countryName": "France"
      }
  ]
}

All I need is to put new value for the key continentName and coutryName from the node and parentOrganizations.address

"address": {
        "continentId": 5,
        "continentName": "South & Central America",
        "countryId": 33,
        "countryName": "Argentina",

Below is my way

def raw = loadResourceContent("createOrganization1.json") --> loading the Json file
def body = new JSONObject(raw)
                .put("dunsNumber", dunsNumberUnique) //--> I I am using a unique value 
                 .put("name",organizatioNameUnique) //--> random Organization name
                 .put("countryName","Argentina") //--> Here It would add new entry in the parent Node.
                 .toString()

Thanks for your help and btw I am new in groovy and testing.


Solution

  • Maybe you want to read the Groovy manual about how to process JSON:

    https://groovy-lang.org/processing-json.html

    You could do something like this:

    package de.scrum_master.stackoverflow.q75817755
    
    import groovy.json.JsonOutput
    import groovy.json.JsonSlurper
    
    def json = '''{
      "dunsNumber": 0,
      "branches": 25,
      "url": "www.nbch.com.com",
      "address": {
        "continentId": 5,
        "continentName": "South & Central America",
        "countryId": 20,
        "countryName": "Brasil"
      },
      "parentOrganizations": [
        {
          "parentType": "IMMEDIATE",
          "name": "abcde",
          "dunsNumber": "12345",
          "address": {
            "continentId": 5,
            "continentName": "Europe",
            "countryId": 33,
            "countryName": "France"
          }
        }
      ]
    }'''
    
    Map parsed = new JsonSlurper().parseText(json)
    parsed.parentOrganizations[0].tap {
      dunsNumber = 424242
      name = "ACME Inc."
      address.continentName = "South & Central America"
      address.countryName = "Argentina"
    }
    println JsonOutput.prettyPrint(JsonOutput.toJson(parsed))
    

    The console log says:

    {
        "dunsNumber": 0,
        "branches": 25,
        "url": "www.nbch.com.com",
        "address": {
            "continentId": 5,
            "continentName": "South & Central America",
            "countryId": 20,
            "countryName": "Brasil"
        },
        "parentOrganizations": [
            {
                "parentType": "IMMEDIATE",
                "name": "ACME Inc.",
                "dunsNumber": 424242,
                "address": {
                    "continentId": 5,
                    "continentName": "South & Central America",
                    "countryId": 33,
                    "countryName": "Argentina"
                }
            }
        ]
    }
    

    Is this what you had in mind?