Reputation: 13
I get a json of below type from a rest service and I need ot parse it and get data through it. This json is an invalid in terms of Json format, but is there way to parse this or ignore a line which has comma
{
"yshan" : "EMP",
"yuansshi" : "EMP",
"zacharyb" : "EMP",
"zholin" : "EMP",
"fy16interns_bxb" : {,
"avaderiy" : "EMP",
"crellis" : "EMP",
}
}
My main intention is to get all the "key" field names which has the value "EMP" whether they are nested or regular json?
I used jsonnode and jsonNode.getFields() for retrieval. This works fine for the json which dont have nested elements, but for the one's which have nested elements it throws "unexpected character" error like
Unexpected character (',' (code 44)): was expecting either valid name character (for unquoted name) or double-quote (for quoted) to start field name
If you can help me out, it would be appreciable
Upvotes: 0
Views: 4781
Reputation: 159135
If you just want to extract keys where value is "EMP"
, you can use regex:
"([^"]+)"\s*:\s*"EMP"
As Java code, that would be:
Matcher m = Pattern.compile("\"([^\"]+)\"\\s*:\\s*\"EMP\"").matcher(input);
while (m.find()) {
System.out.println(m.group(1));
}
See regex101.
Upvotes: 0
Reputation: 114807
Dropping the line would result in even worse json.
If the error is always the same, maybe like it's always a curly bracket followed by a comma, then you could try
json.replace("{,", "{")
)Of course: only if fixing the API is impossible and parsing the 'patched' response makes sense.
Upvotes: 1