1. Introduction

Everyday development involves JSON processing, and the need to add additional fields or remove specific fields from JSON. Today we will use the Jackson class library to implement this functionality.

2. Add additional fields to the JSON string

Suppose we have JSON structured like this:

{
    "username":"felord.cn"."age":18
}
Copy the code

Expect to add a gender field gender:

{
    "username": "felord.cn"."age": 18."gender": "male"
}
Copy the code

First use ObjectMapper to load the JSON string as ObjectNode:

ObjectNode jsonNodes = objectMapper.readValue(json, ObjectNode.class);
Copy the code

ObjectNode provides a number of methods for manipulating JSON properties:

  • getGets the corresponding value based on the index or field nameJsonNode.
  • Put family of methodsProvides a way to combine basic types,nullThe ability to add values, objects, arrays, and raw values to JSON.

We can use the put method to add additional fields.

String json = "{\n" +
        " \"username\":\"felord.cn\",\n" +
        " \"age\":18\n" +
        "}";

ObjectMapper objectMapper = new ObjectMapper();

ObjectNode jsonNodes = objectMapper.readValue(json, ObjectNode.class);
jsonNodes.put("gender"."male");
String newJson = objectMapper.writeValueAsString(jsonNodes);
// newJson = {"username":"felord.cn","age":18,"gender":"male"}
Copy the code

3. Add a field when the object is converted to JSON

Sometimes we define objects that do not contain specific fields, but we also need additional fields when we convert them to JSON. Similar to Section 2, except instead of JSON strings, Java objects are provided:

/ * * *@author felord.cn
 * @since11:02 * /
@Data
public class User {
    private String username;
    private Integer age;
}
Copy the code

The idea is pretty straightforward, as long as we can convert an object to an ObjectNode. ObjectMapper provides the valueToTree method to do this.

User user = new User();
user.setUsername("felord.cn");
user.setAge(18);

ObjectMapper objectMapper = new ObjectMapper();
ObjectNode jsonNodes = objectMapper.valueToTree(user);
jsonNodes.put("gender"."male");
String newJson = objectMapper.writeValueAsString(jsonNodes);
// newJson = {"username":"felord.cn","age":18,"gender":"male"}
Copy the code

4. Remove attributes

To remove properties from a JSON string or Java object, just call the remove method, which is not shown here.

5. Extension

All of the above operations are done with the help of the JSON node class derived from JsonNode in Jackson, as follows:

JsonNode provides fine-grained access to JSON information and provides a very useful JSON object manipulation API, but most people don’t think of it when they need it, so you don’t need to remember the API specifically, just that it can manipulate JSON.

6. Summary

In this paper, Jackson dynamic add, delete, change query JSON was introduced, traction out a very important operation tool JsonNode. Solve problems with the resources you already have, without having to build your own wheels or introduce new dependencies. Well today’s share here, more attention: code farmer xiao Pangge timely get more effective programming dry goods.

Follow our public id: Felordcn for more information

Personal blog: https://felord.cn