0

I have some JSON data like the one below and want to add another county with a city name. How do I add it?

My current JSON data looks like the following:

{
    "state" : "WA",
    "county" : {
        "king" : {
            "Seattle" : [ "r", "d", "n" ],
            "Kirkland" : [ "r", "d", "w" ]
        },
        "queen" : {
            "Edmonds" : [ "r" ]
        }
    }
}

Expected JSON data should look like the following:

{
    "state" : "WA",
    "county" : {
        "king" : {
            "Seattle" : [ "r", "d", "n" ],
            "Kirkland" : [ "r", "d", "w" ]
        },
        "queen" : {
            "Edmonds" : [ "r" ]
        }
        "prince" : {
            "Lynnwood" : [ "r", "d", "w" ]
        }
    }
}
1

2 Answers 2

2

Using the "Append json into a json in groovy", I was able to get it to work.

import groovy.json.*

String[] myArray = [ "r", "d", "w" ]

def builder = new JsonBuilder()
def root = builder.event{                
    "Lynnwood" myArray
}

def json = new JsonSlurper().parseText('''{ "state" : "WA", "county" : { "king" : { "Seattle" : [ "r", "d", "n" ], "Kirkland" : [ "r", "d", "w" ] }, "queen" : { "Edmonds" : [ "r" ] } } }''')

// Append the built JSON to the "slurped" JSON
json.county.prince = root.event

// Re-build the JSON so it can saved as a String
new JsonBuilder(json).toPrettyString()
Sign up to request clarification or add additional context in comments.

Comments

0

You can get your answer from here :- Append json into a json in groovy

Comments

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.