7

Scenario :

I am using following code to decode a JSON String to generate the objects using it.

enter image description here

{"av":{"tid":"1000","sslist":[{"ss":{"ssId":"1","ssName":"Test ss "name one"}},{"ss":{"ssId":"2","ssName":"Test ss name two"}}],"hl":{"lc":0}}}

Now, I have hundreds elements of sslist and all are failed because of one bloody " in ssName in the first ss element.

But this error is throwing when the String is parsing at the very beginning at the createJsonParser() method.

Question :

I want to ignore the error prone node only and proceed with other hundred of correct nodes. Is there any other method to do this?

Code :

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.JsonNode;

public class JsonReader {

        private void readJsonMethodThree(String jsonString) throws JsonParseException, IOException{

            ObjectMapper    mapper          = new ObjectMapper();
            JsonFactory     factory         = mapper.getJsonFactory();
            JsonParser      jsonParser      = factory.createJsonParser(jsonString);
            JsonNode        jsonNode        = mapper.readTree(jsonParser);

        }

}

Exception :

Exception in thread "main" com.fasterxml.jackson.core.JsonParseException: Unexpected character ('n' (code 110)): was expecting comma to separate OBJECT entries
 at [Source: java.io.StringReader@12cc95d; line: 1, column: 69]
ironwood
  • 8,936
  • 15
  • 65
  • 114
  • You should escape the text in each property before sending it to the browser as JSON. – crush Jul 12 '13 at 12:28
  • Thank you. But I'm looking for a method to handlle it when I'm reading it. – ironwood Jul 12 '13 at 12:29
  • 4
    No, your JSON producer should take care of this – epoch Jul 12 '13 at 12:30
  • 1
    validate your json on server end before reach to the client, you can't parse invalid json using any json parser.better use ObjectMapper like class to generate json in server same as client so it will validate it before posting to the client side. – Supun Sameera Jul 12 '13 at 12:46
  • Thank you Sameera, epoch and crush. Actually, the input values are taken from various sources like DB, XMLs etc. And the JSON has been created as a String (without using JSON Objects) by scratch in the server side. I tried to do it without touching the server code. But now I'm re-factoring the server side implementation since there's no other option. – ironwood Jan 16 '14 at 04:12

2 Answers2

4
yourjsonstring = yourjsonstring.replaceAll("\"name", "name");

or use more generic pattern, that is just example.

  • 2
    While your solution may address the current problem, the actual solution is to get the producer to generate valid JSON. – hd1 Jul 12 '13 at 20:07
  • Totally agree. But such solution was mentioned before and the question is still not closed. May be there isn't access to the producer, or a lot of bad-json consumers are already created. so such cosmetic solution might be the only one :) –  Jul 13 '13 at 22:24
  • Thank you Danila! I tried this too. But there were many Bad characters causing this error. So, I thought that I must not be the first one to facee this like problem and there may be a pre-defined libraries / packages to handle these sort of issues. That's y I asked. I tried to do it without touching the server code. But now I'm re-factoring the server side implementation since there's no other option. – ironwood Jan 16 '14 at 04:14
0

You may also get this error when the data type of the JSON does not match the one declared in cassandra. Specifically, check if the JSON value should actually be a list type instead of a singleton.

Ricky Sahu
  • 23,455
  • 4
  • 42
  • 32