Home > OS >  How do I parse rows of json data (via a Groovy script) with identical parent element names?
How do I parse rows of json data (via a Groovy script) with identical parent element names?

Time:10-17

I am writing a Groovy script to parse the following json string. I am brand new to Groovy and Java, so if I'm missing something obvious, then I will thank you in advance for your patience.

[
  {
    "forms": [
      {
        "form1": {
          "title": "Form 1 Title",
          "data": {}
        }
      },
      {
        "form2": {
          "title": "Form 2 Title",
          "data": {}
        }
      },
      {
        "form2": {
          "title": "Form 2 Title",
          "data": {
            "gridData": {
              "id": 1,
              "fullGridId": "1",
              "columns": {
                "col1": "Cust Name",
                "col2": "Cust Number",
                "col3": "Cust Email"
              },
              "rowset": [
                {
                  "col1": "Jane Eyre",
                  "col2": 11111,
                  "col3": "[email protected]"
                },
                {
                  "col1": "Jack Russell",
                  "col2": 22222,
                  "col3": "[email protected]"
                },
                {
                  "col1": "Abraham Lincoln",
                  "col2": 33333,
                  "col3": "[email protected]"
                },
                {
                  "col1": "Muhammad Ali",
                  "col2": 44444,
                  "col3": "[email protected]"
                }
              ],
              "summary": {
                "records": 4
              }
            }
          }
        }
      },
      {
        "form3": {
          "title": "Form 3 Title",
          "data": {}
        }
      }
    ]
  }
]

I need to read through all of the Customer information from "form2" ("Cust Name", "Cust Number" and "Cust Email"). The problem I think I am having is that the parent element "form2" occurs twice in this json. The first occurrence has no data, but the data I need is in the second occurrence. Here is the excerpt from my code where I read the json above. The variable "formData" is a String that contains the json string above.

  def jsonData = new JsonSlurper().parseText(formData);
  def form2Rowset = jsonData.forms.form2.data.gridData.rowset;

  for(i=0,i<rowset.size(),i  )
  {
     rowset[i].get("col1") ....do something
     rowset[i].get("col2") ....do something
     rowset[i].get("col3") ....do something
  }
  return;

I believe the problem is that "form2" occurs twice within the json string, but I am uncertain how to deal with that. I am looking for a solution that does not require downloading/importing any additional packages (if possible?) - I already have JsonSlurper. If you have ideas, please describe the specific code, since I am new, and may not track the concepts without specific code. I won't have any problem tailoring your code suggestions to my situation, but specific code would be most helpful.... and appreciated!!

CodePudding user response:

note that in json you have arrays [...] and maps {...}

def jsonData = new groovy.json.JsonSlurper().parseText(formData)

jsonData.each{root->
  root.forms.each{form->
    form.each{formKey,formValue->
      formValue.data?.gridData?.rowset?.each{row->
        row.col1 = row.col1.toUpperCase()
        println "${formKey} title:: ${formValue.title} row:: ${row}"
      }
    }
  }
}

the same but with one nested each replaced with classic for

jsonData.each{root->
  root.forms.each{form->
    form.each{formKey,formValue-> 
      //formValue.data?.gridData?.rowset?.each{row->
      def rowset = formValue.data?.gridData?.rowset
      if (rowset) {
        for(int i=0;i<rowset.size();i  ){
          rowset[i].col1 = rowset[i].col1.toUpperCase()
          println "${formKey} title:: ${formValue.title} row:: ${rowset[i]}"
        }
      }
    }
  }
}
  • Related