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?

4
  • 2
    groovy-lang.org/json.html Commented Nov 26, 2017 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 Commented Nov 26, 2017 at 15:53
  • It says : No signature of method: groovy.json.JsonSlurper.parseText() is applicable for argument types: (File) values: [C:\Users..........] Commented Nov 26, 2017 at 15:55
  • stackoverflow.com/a/19533616/1759845 Commented Nov 26, 2017 at 15:57

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

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.