4

I have a 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 am trying to parse this using JsonSlurper but getting error:

groovy.json.JsonException: Lexing failed on line: 1, column: 5, while reading 'i', no possible valid JSON value or punctuation could be recognized.

How do I parse it and access the id:'3425'?

0

1 Answer 1

6

Your Json is invalid, you need to use double quote to delimit strings and you also need to put quotes around the keys in your Json like 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 then do:

def ids = new groovy.json.JsonSlurper().parseText( json ).id
assert ids[ 1 ] == '3425'
Sign up to request clarification or add additional context in comments.

3 Comments

first I thought so, but jsonviewer.stack.hu tells you that the Json is a valid one. Anyway thank you for the response.
@asp_NewBee: if the jsonviewer app says it is valid then it's wrong. the spec (json.org) is pretty clear.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.