-2

I have a class with such structure:

class SomeClass {

        private String stringValue; 
        private Collection<String> collectionValue = new ArrayList<>();
        private String jsonStringValue;
        private boolean booleanValue;
}

And then I use

objectMapper.readValue(jsonString, SomeClass.class);

to parse this object from JSON.

The main problem is that jsonStringValue is a json inside of json:

{"stringValue" : "someString",
 "collectionValue" : ["123456", "234567", "hello"],
 "jsonStringValue" : "{
    "someKey" : 1,
    "anotherKey" : {
      "againKey" : "value"
     }
  },
  "booleanValue" : true
}

And trying to parse jsonStringValue it throws

com.fasterxml.jackson.core.JsonParseException: Unexpected character ('a' (code 97)): was expecting comma to separate Object entries

Exactly "a" character from my example (json modified on security purposes)

I believe there should be some escaping rule for parsing json as a String.

How do I parse json value as a string?

Oleksandr Riznyk
  • 758
  • 1
  • 8
  • 19

1 Answers1

1

First, your JSON string is not valid because there is a redundant double quote before the left bracket in jsonStringValue. The valid one looks like this:

{
  "stringValue" : "someString",
  "collectionValue" : ["123456", "234567", "hello"],
  "jsonStringValue" : {
    "someKey" : 1,
    "anotherKey" : {
      "againKey" : "value"
    }
  },
  "booleanValue" : true
}

Second, jsonStringValue is not a simple String object, it is a nested JSON objects. Therefore, you are supposed to create corresponding classes for it as follows:

Class SomeClass {
    private String stringValue; 
    private List<String> collectionValue = new ArrayList<>();
    private JsonStringValue jsonStringValue;
    private boolean booleanValue;

    //general getters and setters
}

Class JsonStringValue  {
    private int someKey;
    private AnotherKey anotherKey;

    //general getters and setters
}

Class AnotherKey {
    private String againKey;

    //general getters and setters
}

At last, the given JSON string can be transformed into SomeClass POJO with ObjectMapper.

ObjectMapper mapper = new ObjectMapper();
SomeClass someClass = mapper.readValue(jsonStr, SomeClass.class);
System.out.println(someClass.getjsonStringValue().getAnotherKey().getAgainKey());

Console output:

value

UPDATED

If you still want to transform the jsonStringValue object into String, an alternative way is shown as follows:

Create 2 classes - SomeClassOriginal and SomeClass, the only difference between them is the data type of jsonStringValue. The former one is JsonNode and later one is String.

Class SomeClassOriginal {
    private String stringValue; 
    private List<String> collectionValue = new ArrayList<>();
    private JsonNode jsonStringValue;
    private boolean booleanValue;

    //general getters and setters
}

Class SomeClass {
    private String stringValue; 
    private List<String> collectionValue = new ArrayList<>();
    private String jsonStringValue;
    private boolean booleanValue;

    public SomeClass(SomeClassOriginal someClassOriginal) {
        super();
        this.stringValue = someClassOriginal.stringValue;
        this.collectionValue = someClassOriginal.collectionValue ;
        this.jsonStringValue= someClassOriginal.jsonStringValue.toString();
        this.booleanValue= someClassOriginal.booleanValue;
    }

    //general getters and setters
}

Then you can get the jsonStringValue as String like this:

ObjectMapper mapper = new ObjectMapper();
SomeClassOriginal someClassOriginal = mapper.readValue(jsonStr, SomeClassOriginal.class);
SomeClass someClass = new SomeClass(SomeClassOriginal);
System.out.println(someClass.getjsonStringValue());

Console output:

{"someKey":1,"anotherKey":{"againKey":"value"}}

LHCHIN
  • 3,679
  • 2
  • 16
  • 34
  • Thanks for your answer but unfortunately I need to parse nested JSON object like a plain String because it can have various structure which is not need to be parsed on my side because it is handled by 3rd party library. – Oleksandr Riznyk Oct 01 '19 at 07:49
  • @Oleksandr Riznyk Hi, I have updated the answer, please check! – LHCHIN Oct 01 '19 at 08:37