Jigs Jigs - 3 months ago 12
Groovy Question

How do I get "keys" from an unkown .json saved in my computer using groovy

My end goal is to parse thru an unknown .json file stored on my laptop and get the key names (not the values only key names) using Groovy in SoapUI.

I want to parse an unknown JSON file stored in my computer and get its keys (name of the keys, not the values). I can do 2 things separately:


  1. I am able to read the local JSON using the following code I found online:

    def JSON_URL = "file:///C:/Users/xxx/example.json"
    URL url = new URL(JSON_URL)
    InputStream urlStream = null

    try {
    urlStream = url.openStream()
    BufferedReader reader = new BufferedReader(new Inpu tStreamReader(urlStream))
    JsonSlurper js1 = new JsonSlurper()
    Object result = js1.parse(reader)
    log.info "==> readJSONfile2 result of read: "+result
    } catch (Exception e) {
    log.info e
    }

  2. And if I have the URL, I am able to parse it and get keys like so:

    // getting the response

    def resp1 = context.expand('${testStepName#response}')


    // parsing the set context

    def js1 = new JsonSlurper().parseText(resp1)

    def keys = js1.entrySet() as List

    log.info "==> runTreatmentPlan keys list is: "+keys

    log.info "==> runTreatmentPlan keys size is: "+keys.size()



But I am unable to get the keys if the JSON is local to my machine ie unable to combine both the codes. I get error when I do:

Object result = js1.parseText(reader)


I am new to groovy and SoapUI and json - a total newbie and this is my first question. I am really scared because I have seen that some people are kinda rough towards other newbies if the question is basic. I promise, I did google a lot, and I am sure the experienced people might find my question stupid as well, but I am really stuck. I am unable to combine both pieces of code. I kinda feel that I will some how have to use the response from #1 code, but I don't know how.
Can someone help me please?

==== Updating with the JSON structure:

{
"Key0": [
{
"Key1": "Value1",
"Key2": "Value2",
"Key3": "Value3",
"Key4": {
"subKey1": "subValue1"
},
"Key5": {
"subKey1": "subValue1",
"subKey2": "subValue2",
"subKey3": "subValue3",
"subKey4": "subValue4"
},
"Key6": "2016-07-11T17:52:59.000Z",
"Key7": [
{
"subKey1": "subValue1",
"subKey2": "subValue2"
},
{
"subKey1": "subValue1",
"subKey2": "subValue2"
},
{
"subKey1": "subValue1",
"subKey2": "subValue2"
},
{
"subKey1": "subValue1",
"subKey2": "subValue2"
}
]
},
{
"Key1": "Value1",
"Key2": "Value2",
"Key3": "Value3",
"Key4": {
"subKey1": "subValue1"
},
"Key5": {
"subKey1": "subValue1",
"subKey2": "subValue2",
"subKey3": "subValue3",
"subKey4": "subValue4"
},
"Key6": "2016-07-11T17:52:59.000Z",
"Key7": [
{
"subKey1": "subValue1",
"subKey2": "subValue2"
},
{
"subKey1": "subValue1",
"subKey2": "subValue2"
},
{
"subKey1": "subValue1",
"subKey2": "subValue2"
},
{
"subKey1": "subValue1",
"subKey2": "subValue2"
}
]
}
]
}

Answer

Given your input, this code:

import groovy.json.JsonSlurper

def traverse
traverse = { tree, keys = [], prefix = '' ->
    switch (tree) {
        case Map:
            tree.each { k, v -> 
                def name = prefix ? "${prefix}.${k}" : k
                keys << name
                traverse(v, keys, name)
            }
            return keys
        case Collection:
            tree.eachWithIndex { e, i -> traverse(e, keys, "${prefix}[$i]") }
            return keys
        default :
            return keys
    }
}

def content = new JsonSlurper().parse( new File( 'sample.json' ) )

traverse(content).each { println it }

Yields this output:

Key0
Key0[0].Key1
Key0[0].Key2
Key0[0].Key3
Key0[0].Key4
Key0[0].Key4.subKey1
Key0[0].Key5
Key0[0].Key5.subKey1
Key0[0].Key5.subKey2
Key0[0].Key5.subKey3
Key0[0].Key5.subKey4
Key0[0].Key6
Key0[0].Key7
Key0[0].Key7[0].subKey1
Key0[0].Key7[0].subKey2
Key0[0].Key7[1].subKey1
Key0[0].Key7[1].subKey2
Key0[0].Key7[2].subKey1
Key0[0].Key7[2].subKey2
Key0[0].Key7[3].subKey1
Key0[0].Key7[3].subKey2
Key0[1].Key1
Key0[1].Key2
Key0[1].Key3
Key0[1].Key4
Key0[1].Key4.subKey1
Key0[1].Key5
Key0[1].Key5.subKey1
Key0[1].Key5.subKey2
Key0[1].Key5.subKey3
Key0[1].Key5.subKey4
Key0[1].Key6
Key0[1].Key7
Key0[1].Key7[0].subKey1
Key0[1].Key7[0].subKey2
Key0[1].Key7[1].subKey1
Key0[1].Key7[1].subKey2
Key0[1].Key7[2].subKey1
Key0[1].Key7[2].subKey2
Key0[1].Key7[3].subKey1
Key0[1].Key7[3].subKey2