Bhanuchander Udhayakumar
Bhanuchander Udhayakumar

Reputation: 1646

How to get the exact json node instance using groovy?

Input Json file :

{
   "menu": {
     "id": "file",
     "value": "File",
     "popup": {
          "menuitem": [
               {
                   "value": "New", 
                   "onclick": ["CreateNewDoc()","hai"],
                   "newnode":"added"
               }
          ]
      }
  }
}

Groovy code :

def newjson = new JsonSlurper().parse(new File ('/tmp/test.json'))
def value=newjson.menu.popup.menuitem.value
def oneclick=newjson.menu.popup.menuitem.onclick
println value
println value.class
println oneclick
println oneclick.class

Output:

[New]
class java.util.ArrayList
[[CreateNewDoc(), hai]]
class java.util.ArrayList

Here,
The json nodes which carries String and List returns the same class name with the groovy code above shown.

Update 1:

I don't exactly know, can do this like shown below. My expectation to get the results this,

New
class java.util.String
[CreateNewDoc(), hai]
class java.util.ArrayList

Upvotes: 3

Views: 3595

Answers (3)

daggett
daggett

Reputation: 28634

in your json the menuitem contains array of one object:

      "menuitem": [
           {
               "value": "New", 
               "onclick": ["CreateNewDoc()","hai"],
               "newnode":"added"
           }
      ]

and when you try to access menuitem.value groovy actually returns a list of value attributes for all objects in menuitem array.

that's why menuitem.value returns array ["New"]

in this case

      "menuitem": [
           {
               "value": "New", 
               "onclick": ["CreateNewDoc()","hai"],
               "newnode":"added"
           },
           {
               "value": "Old", 
               "onclick": ["CreateOldDoc()","hai"],
               "newnode":"added"
           }
      ]

menuitem.value will return array ["New", "Old"]

but menuitem[0].value will return the string value "New"

so in your groovy code to get attributes of first menu item:

def value=newjson.menu.popup.menuitem[0].value
def oneclick=newjson.menu.popup.menuitem[0].onclick

Upvotes: 1

Evgeny Smirnov
Evgeny Smirnov

Reputation: 3026

menuitem is list, so you need to get property on concrete list element:

assert newjson.menu.popup.menuitem instanceof List
assert newjson.menu.popup.menuitem[0].value instanceof String
assert newjson.menu.popup.menuitem[0].onclick instanceof List

Upvotes: 1

Rao
Rao

Reputation: 21389

Here you go:

  • In the below script using closure to show the details of each value and its type
  • Another closure is used to show the each map in the menuitem list.
def printDetails = { key, value -> println "Key - $key, its value is \"${value}\" and is of typpe ${value.class}" }

def showMap = { map -> map.collect { k, v -> printDetails (k,v) } }

def json = new groovy.json.JsonSlurper().parse(new File('/tmp/test.json'))
def mItem = json.menu.popup.menuitem
if (mItem instanceof List) {
   mItem.collect { showMap it  }
}
println 'done'

You can quickly try the same online demo

Upvotes: 2

Related Questions