kashivivek
kashivivek

Reputation: 13

can we ignore a line in json parsing?

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

Answers (2)

Andreas
Andreas

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

Andreas Dolk
Andreas Dolk

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

  1. read the json response in a string
  2. clean up the string (example: json.replace("{,", "{"))
  3. parse the cleaned string.

Of course: only if fixing the API is impossible and parsing the 'patched' response makes sense.

Upvotes: 1

Related Questions