Roee Gavirel
Roee Gavirel

Reputation: 19463

Using jackson to manipulate part of a JSON

I have a JSON object, something like:

{
    "myData":[1,2,3],
    "externalData1":...,
    ...
    "externalDataN":...
}

I want to read this JSON, update my data (lets say adding number "4") and save the JSON without knowing anything about the external data fields.

I know that for parsing I can use the @JsonIgnoreProperties but then I will loss the data on the save. Is there a way to do that without going manually over the TreeModel?

Thanks.

Upvotes: 2

Views: 680

Answers (2)

cy3er
cy3er

Reputation: 1699

Use org.json:

JSONObject root = new JSONObject(json);

JSONArray myData = (JSONArray) root.get("myData");

myData.remove(0);
myData.put(4);

System.out.println("root = " + root.toString());

Upvotes: 2

Simon
Simon

Reputation: 6363

You can use json-simplewhich won't give you the data binding to a POJO, but it will still be a lot more convenient than doing the parsing your self.

Upvotes: 1

Related Questions