0

I want to parse this data using JSONSlurper and convert it into a map and then pass data using variable to a template.

{ 
 "biodata": {
    "Ruby": {
      "Expertise": "web development",
      "EXperience": "5 years"
    },
    "Dylon": {
      "Expertise": "Java",
      "EXperience": "2 years"
    }
 }
}

Something like this:

def myJson = fetchedJson

def experienceDylon = myJson.biodata.dylon.experience 

How to achieve this using groovy?

doelleri
  • 19,232
  • 5
  • 61
  • 65
jane
  • 211
  • 9
  • 30
  • 2
    http://groovy-lang.org/json.html – BackSlash Nov 26 '17 at 15:17
  • Thanks for the input . When I try to use this I get error : def files = new File("C:/Users/.........................") def jsonSlurper = new JsonSlurper() def object = jsonSlurper.parseText(files) println "${object}" assert object instanceof Map – jane Nov 26 '17 at 15:53
  • It says : No signature of method: groovy.json.JsonSlurper.parseText() is applicable for argument types: (File) values: [C:\Users..........] – jane Nov 26 '17 at 15:55
  • https://stackoverflow.com/a/19533616/1759845 – BackSlash Nov 26 '17 at 15:57

1 Answers1

2

Consider this example:

import groovy.json.*

def file = new File("importData.json")
def myJson = new JsonSlurper().parse(file)

// note original JSON has 'EXperience'
def result =  myJson['biodata']['Dylon']['EXperience']
assert '2 years' == result
Michael Easter
  • 23,733
  • 7
  • 76
  • 107