Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding an array element to JSON using Jackson

Tags:

java

json

jackson

I have a JSON that looks like this

[
   {
      "itemLabel":"Social Media",
      "itemValue":90
   },
   {
      "itemLabel":"Blogs",
      "itemValue":30
   },
   {
      "itemLabel":"Text Messaging",
      "itemValue":60
   },
   {
      "itemLabel":"Email",
      "itemValue":90
   },
]

I want to place all of those objects into an array to manipulate it easier in one of my code. Thus I want to do something like

[
    {
        "data": [
            {
                "itemLabel": "Social Media",
                "itemValue": 90
            },
            {
                "itemLabel": "Blogs",
                "itemValue": 30
            },
            {
                "itemLabel": "Text Messaging",
                "itemValue": 60
            },
            {
                "itemLabel": "Email",
                "itemValue": 90
            }
        ]
    }
]

How do I go about to add in that data array element using Jackson? I have done mostly read using Jackson but have not done too many writes. Any help would be appreciated.

like image 297
cYn Avatar asked Apr 23 '14 16:04

cYn


People also ask

How does Jackson read JSON array?

Reading JSON from a File Thankfully, Jackson makes this task as easy as the last one, we just provide the File to the readValue() method: final ObjectMapper objectMapper = new ObjectMapper(); List<Language> langList = objectMapper. readValue( new File("langs. json"), new TypeReference<List<Language>>(){}); langList.

How do you add an array to a JSON object in Java?

We can also add a JSONArray to JSONObject. We need to add a few items to an ArrayList first and pass this list to the put() method of JSONArray class and finally add this array to JSONObject using the put() method.

Does Jackson preserve array order?

Jackson mapper fill the ArrayList maintaining the order of JSON. If you want a different order you can use the annotation @JsonPropertyOrder.

How to add an array element to JSON object in JavaScript?

How to add an array element to JSON object in JavaScript. 1 Parse the JSON object to create a native JavaScript Object. 2 Push new array element into the object using .push () 3 Use stringify () to convert it back to its original format.

How do I convert a JSON array to a java array?

In this article, we'll convert a JSON array into a Java Array and Java List using Jackson. Since we're using Jackson, you'll have to add it to your project. If you're using Maven, it's as easy as adding the dependency: Since we're mapping from JSON to our own objects, let's go ahead and define a POJO:

What are the types of arrays in JSON?

1 Arrays as JSON Objects. Arrays in JSON are almost the same as arrays in JavaScript. In JSON, array values must be of type string, number, object, array, boolean or null. 2 Arrays in JSON Objects 3 Accessing Array Values 4 Looping Through an Array 5 Nested Arrays in JSON Objects 6 Modify Array Values 7 Delete Array Items

How do you read a JSON object in Jackson?

Jackson allows you to read JSON into a tree model: Java objects that represent JSON objects, arrays and values. These objects are called things like JsonNode or JsonArray and are provided by Jackson. Jackson uses a class called ObjectMapper as its main entrypoint.


1 Answers

I'm not completely sure what are you intending and there is probably a more elegant solution to this (using POJOs rather than Collections and Jacksons JSON representation), but I guess this example will clear it out to you. But if you have some more complicated processing you might want to write custom (de)serializers or something like that. Written using Jackson 2.3.3

ObjectMapper mapper = new ObjectMapper();
JsonNode parsedJson = mapper.readTree(json); //parse the String or do what you already are doing to deserialize the JSON
ArrayNode outerArray = mapper.createArrayNode(); //your outer array
ObjectNode outerObject = mapper.createObjectNode(); //the object with the "data" array
outerObject.putPOJO("data",parsedJson); 
outerArray.add(outerObject);
System.out.println(outerArray.toString()); //just to confirm everything is working
like image 129
Aleksandar Stojadinovic Avatar answered Oct 09 '22 00:10

Aleksandar Stojadinovic