3

I am pretty new to json and normally donot use do much coding. I want a service which takes below json string as input

{
    "var": "test11",
    "_env": {
        "activation": "wm6a93e3a80-0307-12cc-96e6-d79883bf841a",
        "uuid": "48cdc2d0-0212-11e6-8315-d79883bf841a",
        "eventID": 49167,
        "recvTime": "Thu Apr 14 00:27:03 PDT 2016"
    }
}

and spit out output as

{
    "var": "test11"
} 

and

{
    "_env": {
        "activation": "wm6a93e3a80-0307-12cc-96e6-d79883bf841a",
        "uuid": "48cdc2d0-0212-11e6-8315-d79883bf841a",
        "eventID": 49167,
        "recvTime": "Thu Apr 14 00:27:03 PDT 2016"
    }
}

The is just an example. It can contain more objects in json string and _env won't always appear at the end.

Is there any simple way to achieve using jackson API ?

1
  • Do you know the name of all the keys in the original json and the keys that you want to separate out? Commented Apr 14, 2016 at 8:16

2 Answers 2

3

Create a class to hold your JSON, like below:

public class Bar {

    private String var;

    @JsonProperty("_env")
    private Object env;

    public String getVar() {
        return var;
    }

    public void setVar(String var) {
        this.var = var;
    }

    public Object getEnv() {
        return env;
    }

    public void setEnv(Object env) {
        this.env = env;
    }
}

And set the mapper to not fail on unknown properties before deserializing;

public static void main(String[] argv) throws Exception {
    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

    Bar bar = mapper.readValue(JSON, Bar.class);

}

This way, both when the _env is present or not, it will work.

If you need to "split" the json by each node, you can do something like this:

JsonNode node = mapper.readTree(JSON);
List<String> nodeJsons = new ArrayList<>();
Iterator<Entry<String, JsonNode>> nodeIterator = node.fields();
while (nodeIterator.hasNext()) {
    Map.Entry<String, JsonNode> entry = nodeIterator.next();
    nodeJsons.add(mapper.writeValueAsString(entry));
}

This way you will have a list of json strings in the end with every node serialized by itself, instead of one "big" json with everything.

Sign up to request clarification or add additional context in comments.

Comments

1

with dynamic input, it is best to load json into a Map:

public static void main(String[] args)
{
    ObjectMapper mapper = new ObjectMapper();
    try (Reader reader = new FileReader("C://Temp/xx.json")) {
        // load whatever json into Map
        @SuppressWarnings("unchecked")
        Map<String, Object> inMap = mapper.readValue(reader, Map.class);
        System.out.println(inMap);
        // exmaple for generating json for each key found 
        for (String key : inMap.keySet()) {
            mapper.writeValue(System.out, Collections.singletonMap(key, inMap.get(key)));
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

No need for custom class and obscure jackson configuration. Whatever is in the input gets loaded into the Map. You can produce a json for every separate key, or interogate the map contents any way yyou like

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.