0

I am trying to parse a JSON string which is of the following format

{"edgeNodeRegistrationStatus": ["{\"CONFIRMED\":\"TRUE\"}"]}

I have written a code to parse it.

import groovy.json.JsonOutput
import groovy.json.JsonSlurper

def jsonSlurper = new JsonSlurper()

def object = jsonSlurper.parseText('{"edgeNodeRegistrationStatus": ["{\"CONFIRMED\":\"TRUE\"}"]}')

println(object["edgeNodeRegistrationStatus"][0])

I expect the code to print {"CONFIRMED":"TRUE"}. But its throwing an error

Caught: groovy.json.JsonException: expecting a ',' or a ']',  but got 
the current character of  'C' with an int value of 67  on array index of 1 


The current character read is 'C' with an int value of 67
expecting a ',' or a ']',  but got 
the current character of  'C' with an int value of 67  on array index of 1 

line number 1
index number 35
{"edgeNodeRegistrationStatus": ["{"CONFIRMED":"TRUE"}"]}
...................................^
groovy.json.JsonException: expecting a ',' or a ']',  but got 
the current character of  'C' with an int value of 67  on array index of 1 


The current character read is 'C' with an int value of 67
expecting a ',' or a ']',  but got 
the current character of  'C' with an int value of 67  on array index of 1 

line number 1
index number 35
{"edgeNodeRegistrationStatus": ["{"CONFIRMED":"TRUE"}"]}
...................................^
    at jdoodle.run(jdoodle.groovy:4)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
Command exited with non-zero status 1
4
  • 1
    Assuming you really want JSON inside JSON, you are better off using the already imported JsonOutput to get problems like that out of the way quickly: JsonOutput.toJson([edgeNodeRegistrationStatus: [JsonOutput.toJson([CONFIRMED: true])]]) Commented May 28, 2019 at 14:15
  • @thehole I posted the wrong content earlier Commented May 28, 2019 at 14:17
  • 1
    You need to write \\ inside the ' Commented May 28, 2019 at 14:18
  • @cfrick I am getting the JSON object in the format {"edgeNodeRegistrationStatus": ["{\"CONFIRMED\":\"TRUE\"}"]}. I need to get the status of the CONFIRMED attribute. How can I achieve that? Commented May 28, 2019 at 14:22

2 Answers 2

2

Using \" inside a ''-String will give you just " inside the string itself (same as in a ""-String). But you want to quote the \" for the JSON (not groovy). So you need to use \\" instead.

Unless you really want to have that string for testing, you are better off just generating the JSON you expect there in your code. So you don't have to battle that. E.g.

JsonOutput.toJson([edgeNodeRegistrationStatus: [JsonOutput.toJson([CONFIRMED: "TRUE"])]])
Sign up to request clarification or add additional context in comments.

Comments

1

Or, you can use a different String delimiter, thusly:

def text = $/{"edgeNodeRegistrationStatus": ["{\"CONFIRMED\":\"TRUE\"}"]}/$

def object = jsonSlurper.parseText(text)

println object.edgeNodeRegistrationStatus[0]

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.