Reputation: 4268
i have a json string say:
{"store" : {
"book" : [
{
"category" : "reference",
"author" : "Nigel Rees",
"title" : "Sayings of the Century",
"display-price" : 8.95
},
{
"category" : "fiction",
"author" : "Evelyn Waugh",
"title" : "Sword of Honour",
"display-price" : 12.99
},
{
"category" : "fiction",
"author" : "Herman Melville",
"title" : "Moby Dick",
"isbn" : "0-553-21311-3",
"display-price" : 8.99
},
{
"category" : "fiction",
"author" : "J. R. R. Tolkien",
"title" : "The Lord of the Rings",
"isbn" : "0-395-19395-8",
"display-price" : 22.99
}
]
}
and i would like to add a new json node into this string based on some condition
example: add $.book[0].origin = 'burma' if not present
I have searched for a library that can do that but couldn't find any. I tried JsonPath.
Has anyone used any library that can do fulfill this use case?
Upvotes: 0
Views: 1391
Reputation: 3070
You can achieve what you want using Jackson library. First get rootNode and then child nodes and get the element at given index. And then create new Book object and append it to book array using addPojo method.
Here is the code :
public class Main {
public static void main(String[] args) throws IOException {
File file = new File("test.json");
ObjectMapper mapper = new ObjectMapper();
JsonNode rootNode = mapper.readTree(file);
JsonNode store = rootNode.get("store");
JsonNode books = store.get("book");
// get an element in that node
JsonNode bookElement = books.get(0);
Book book = new Book();
book.setAuthor("test");
book.setCategory("test");
book.setDisplayPrice("test");
book.setTitle("test");
// you can change the logic with editing following lines
// if has the desired field
if (bookElement.hasNonNull("origin")) {
// if field value is not equal to given text
if (!bookElement.get("origin").textValue().equalsIgnoreCase("burma")) {
((ArrayNode)books).addPOJO(book);
}
}
// write to file
mapper.writeValue(file, rootNode);
}
}
Book class :
@Data // comes from lombok for getter-setter
class Book {
private String category;
private String author;
private String title;
@JsonProperty("display-price")
private String displayPrice;
@JsonInclude(JsonInclude.Include.NON_NULL)
private String isbn;
}
If you have field origin and it is not equal to "burma" the file becomes after appending object:
{
"store": {
"book": [
{
"category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"display-price": 8.95
},
{
"category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"display-price": 12.99
},
{
"category": "fiction",
"author": "Herman Melville",
"title": "Moby Dick",
"isbn": "0-553-21311-3",
"display-price": 8.99
},
{
"category": "fiction",
"author": "J. R. R. Tolkien",
"title": "The Lord of the Rings",
"isbn": "0-395-19395-8",
"display-price": 22.99
},
{
"category": "test",
"author": "test",
"title": "test",
"display-price": "test"
}
]
}
}
Jackson maven:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.6.3</version>
</dependency>
Upvotes: 1