Parsing Json in Groovy -
i have following json string:
[ { id: '123', name: 'bla bla', type: 'source', isleaf: true }, { id: '3425', name: 'test test', type: 'reference', isleaf: false }, { id: '12678', name: 'tags', type: 'source', isleaf: false }, ]
i trying parse using jsonslurper getting error:
groovy.json.jsonexception: lexing failed on line: 1, column: 5, while reading 'i', no possible valid json value or punctuation recognized.
how parse , access id:'3425'
?
your json invalid, need use double quote delimit strings , need put quotes around keys in json so:
[ { "id": "123", "name": "bla bla", "type": "source", "isleaf": true }, { "id": "3425", "name": "test test", "type": "reference", "isleaf": false }, { "id": "12678", "name": "tags", "type": "source", "isleaf": false }, ]
you can do:
def ids = new groovy.json.jsonslurper().parsetext( json ).id assert ids[ 1 ] == '3425'