Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The simplest solution which I can imagine - is deserializing your JSON into <code>Map</code> class (for example LinkedHashMap). Please, see my below example:</p> <pre><code>import java.util.LinkedHashMap; import com.fasterxml.jackson.databind.ObjectMapper; public class JacksonProgram { public static void main(String[] args) throws Exception { ObjectMapper mapper = new ObjectMapper(); String json = "{\"a\":\"java.lang.Integer\",\"b\":\"time json\",\"c\":\"action json\",\"d\":[1,2,3]}"; System.out.println(json); LinkedHashMap&lt;String, Object&gt; map = mapper.readValue(json, LinkedHashMap.class); map.put("b", "Override property or create new"); System.out.println(mapper.writeValueAsString(map)); } } </code></pre> <p>Above program prints:</p> <pre><code>{"a":"java.lang.Integer","b":"time json","c":"action json","d":[1,2,3]} {"a":"java.lang.Integer","b":"Override property or create new","c":"action json","d":[1,2,3]} </code></pre> <p>If you want to change inner property on the path you can implement it in this way:</p> <pre><code>import java.io.IOException; import java.util.Arrays; import java.util.Collection; import java.util.LinkedHashMap; import java.util.LinkedList; import com.fasterxml.jackson.databind.ObjectMapper; public class JacksonProgram { public static void main(String[] args) throws IOException { ObjectMapper mapper = new ObjectMapper(); String json = "{\"b\":{\"bb\":{\"bbb\":20}}}"; System.out.println(json); LinkedHashMap&lt;String, Object&gt; map = mapper.readValue(json, LinkedHashMap.class); JsonUpdater updater = new JsonUpdater(map); updater.update(Arrays.asList("b", "bb", "bbb"), 4); System.out.println(mapper.writeValueAsString(map)); } } class JsonUpdater { private LinkedHashMap&lt;String, Object&gt; jsonMap; public JsonUpdater(LinkedHashMap&lt;String, Object&gt; jsonMap) { this.jsonMap = jsonMap; } public boolean update(Collection&lt;String&gt; propertiesOnThePath, Object newValue) { LinkedList&lt;String&gt; path = new LinkedList&lt;String&gt;(propertiesOnThePath); String lastProperty = path.removeLast(); LinkedHashMap&lt;String, Object&gt; objectMap = jsonMap; while (!path.isEmpty()) { String property = path.poll(); if (!objectMap.containsKey(property)) { return false; } objectMap = (LinkedHashMap&lt;String, Object&gt;) objectMap.get(property); } if (!objectMap.containsKey(lastProperty)) { return false; } objectMap.put(lastProperty, newValue); return false; } } </code></pre> <p>Above program prints:</p> <pre><code>{"b":{"bb":{"bbb":20}}} {"b":{"bb":{"bbb":4}}} </code></pre> <p>As we can see: value was changed. But this solution has huge disadvantage - we have to deserialize all JSON. Few thousands bytes String is not a problem for Java, but if you really want to optimaze your program you can play with <code>ObjectNode</code> class and <code>ObjectMapper#readTree</code> method. Please see below source code:</p> <pre><code>import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ObjectNode; public class JacksonProgram { public static void main(String[] args) throws Exception { ObjectMapper mapper = new ObjectMapper(); String json = "{\"a\":\"java.lang.Integer\",\"b\":\"time json\",\"c\":\"action json\",\"d\":[1,2,3]}"; System.out.println(json); ObjectNode jsonTree = (ObjectNode) mapper.readTree(json); jsonTree.put("b", "Override property or create new"); System.out.println(jsonTree.toString()); } } </code></pre> <p>Above program prints:</p> <pre><code>{"a":"java.lang.Integer","b":"time json","c":"action json","d":[1,2,3]} {"a":"java.lang.Integer","b":"Override property or create new","c":"action json","d":[1,2,3]} </code></pre> <p>I didn't do any comparison tests, but you can test which solution works faster for you.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload