1

In groovy how can I create ouput json in the below two structure by using the below input. I have done in java but I'm not getting how can I do in groovy.

INPUT:

{
  "name": "app",
  "test2": [
    {
      "status": [
        {
          "id": 1,
          "pole": [
            {
              "Color": "RED",
              "Status": 0
            },
            {
              "Color": "AMBER",
              "Status": 0
            },
            {
              "Color": "GREEN",
              "Status": 0
            }
          ]
        },
        {
          "id": 2,
          "pole": [
            {
              "Color": "RED",
              "Status": 0
            },
            {
              "Color": "AMBER",
              "Status": 0
            },
            {
              "Color": "GREEN",
              "Status": 0
            }
          ]
        }
      ]
    },
    {
      "status": [
        {
          "id": 1,
          "pole": [
            {
              "Color": "RED",
              "Status": 0
            },
            {
              "Color": "AMBER",
              "Status": 0
            },
            {
              "Color": "GREEN",
              "Status": 0
            }
          ]
        },
        {
          "id": 2,
          "pole": [
            {
              "Color": "RED",
              "Status": 0
            },
            {
              "Color": "AMBER",
              "Status": 0
            },
            {
              "Color": "GREEN",
              "Status": 0
            }
          ]
        }
      ]
    }
  ]
}

Ouput1: One to many output should be produced where by iterating many output of the below structure need to be produced.Like the below output it should produce 12 JSON Object.

{
  "OUTPUT": {
    "STATUS": {
      "id": "1",
      "color": "RED"
    }
  }
}

Output2:

{
      "OUTPUT": {
        "STATUS": [{
          "id": "1",
          "color": "RED"
        },{
          "id": "1",
          "color": "AMBER"
        }
        .
        .
        {
          "id": "2",
          "color": "RED"
        }
      ]
      }
    }

As I'm new to groovy I'm not getting clearly how I can do this.

Below is the code which I have tried so, far.

import groovy.json.*;

def data='''{"name": "app","test2": [{"status": [{"id": 1,"pole": [{"Color": "RED","Status": 0},{"Color": "AMBER","Status": 0},{"Color": "GREEN","Status": 0}]},{"id": 2,"pole": [{"Color": "RED","Status": 0},{"Color": "AMBER","Status": 0},{"Color": "GREEN","Status": 0}]}]},{"status": [{"id": 1,"pole": [{"Color": "RED","Status": 0},{"Color": "AMBER","Status": 0},{"Color": "GREEN","Status": 0}]},{"id": 2,"pole": [{"Color": "RED","Status": 0},{"Color": "AMBER","Status": 0},{"Color": "GREEN","Status": 0}]}]}]}'''


def data1 = new JsonSlurper().parseText( data );

def a=[:]
def OU=[:]
def status=[]
a.OUTPUT=OU
OU.STATUS=status
for(def i=0;i<data1.test2.size();i++){
for(def j=0;j<data1.test2[i].status.size();j++){
    def b=[:]
   b.id=data1.test2[i].status[j].id
   for(def k=0;k<data1.test2[i].status[j].pole.size();k++){
       b.color=data1.test2[i].status[j].pole[k].Color
       status<<b
   }
}
}





String finalJson = JsonOutput.toJson a;
println finalJson

Is this the correct way forming json(using map) as I found apart from JSONBuilder, Map is also another way for creating JSON. Earlier I was using org.json for creating dynamic json normlization script so, if the above method which I used is correct then I can proceed with generating dynamic data normalization from JSON-JSON with the groovy script from java code.

2
  • It's not clear (to me) what the expected output should be. Do you have two sets of desired output? Commented Nov 1, 2020 at 13:09
  • I have updated the question and my code is working can you check is that correct way for creating JSON as I'm new to groovy. I am creating JSON normalization using groovy script but this is dynamically generated by using the input and output JSON mapping and I am creating the groovyscript using JAVA and upload to the engine. So, if the above approach is valid then I'll proceed because in the above approach it'll easy to generate script but if I use JSONBuilder, and usage of closure then it may become difficult to generate. SO, I just need to your input here. Commented Nov 2, 2020 at 7:28

1 Answer 1

2

Not 100% what the output should look like, but the script

import groovy.json.*

def data='''{"name": "app","test2": [{"status": [{"id": 1,"pole": [{"Color": "RED","Status": 0},{"Color": "AMBER","Status": 0},{"Color": "GREEN","Status": 0}]},{"id": 2,"pole": [{"Color": "RED","Status": 0},{"Color": "AMBER","Status": 0},{"Color": "GREEN","Status": 0}]}]},{"status": [{"id": 1,"pole": [{"Color": "RED","Status": 0},{"Color": "AMBER","Status": 0},{"Color": "GREEN","Status": 0}]},{"id": 2,"pole": [{"Color": "RED","Status": 0},{"Color": "AMBER","Status": 0},{"Color": "GREEN","Status": 0}]}]}]}'''

def data1 = new JsonSlurper().parseText data

def output = [ OUTPUT:[
  STATUS:data1.test2.status.inject( [] ){ res, curr ->
    curr.each{ stat ->
      stat.pole.each{
        res << [ id:stat.id, color:it.Color ]
      }
    }
    res
  }
] ]

println JsonOutput.prettyPrint( JsonOutput.toJson( output ) )

produces the following output:

{
    "OUTPUT": {
        "STATUS": [
            {
                "id": 1,
                "color": "RED"
            },
            {
                "id": 1,
                "color": "AMBER"
            },
            {
                "id": 1,
                "color": "GREEN"
            },
            {
                "id": 2,
                "color": "RED"
            },
            {
                "id": 2,
                "color": "AMBER"
            },
            {
                "id": 2,
                "color": "GREEN"
            },
            {
                "id": 1,
                "color": "RED"
            },
            {
                "id": 1,
                "color": "AMBER"
            },
            {
                "id": 1,
                "color": "GREEN"
            },
            {
                "id": 2,
                "color": "RED"
            },
            {
                "id": 2,
                "color": "AMBER"
            },
            {
                "id": 2,
                "color": "GREEN"
            }
        ]
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

I have updated the question and my code is working can you check is that correct way for creating JSON as I'm new to groovy. I am creating JSON normalization using groovy script but this is dynamically generated by using the input and output JSON mapping and I am creating the groovyscript using JAVA and upload to the engine. So, if the above approach is valid then I'll proceed because in the above approach it'll easy to generate script but if I use JSONBuilder, and usage of closure then it may become difficult to generate. SO, I just need to your input here.
yeah thank you but as I am generating script dynamically using JAVA which needs to be deployed. I want the coding pattern like how I have written So, that script generation logic will be easy. So, it would be helpful if you give input like whether is that approach is valid?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.