Please I have List containing below value
[ { "sender" : "sys" , "message" : [ "Hello, " , "how are you?"] ,"transaction_date" : { "$date" : "2019-02-14T17:34:12.705Z"} , "has_suggestions" : true } , { "sender" : "user" , "message" : [ "Hello." , "hey you"] , "visited" : true , "transaction_status" : "Succeded" , "transaction_date" : { "$date" : "2019-02-14T17:34:12.705Z"} , "has_suggestions" : true , "suggestion_list" : [ "Yes, I am." , "No, I am not."] , "suggestion_list_type" : "buttons"}, { "sender" : "sys" , "message" : { "has_form" : true , "form_type" : "prehandover_form" , "data" : { "text" : [ "Kindly provide your info"]}} , "visited" : false , "transaction_status" : "Succeded" , "transaction_date" : { "$date" : "2019-02-14T17:34:15.386Z"}}]
I tried to traverse through the content of the JSON data using Jackson JsonNode below the the code:
ObjectMapper object = new ObjectMapper();
JsonNode jsonNode = object.readTree(value);
String sender = jsonNode.get("sender").textValue();
But am getting a Exception in thread "main" java.lang.NullPointerException
Please any help to traverse through the the JSON data will highly be appreciated.
You need to get the ArrayList from JsonNode.
Here is your solution.
ObjectMapper objectMapper = new ObjectMapper();
JsonNode jsonNode = objectMapper.readTree(json);
String sender = jsonNode.get(0).get("sender").textValue();
JsonNode contains multiple objects. Due to read all sender elements, you may use following code.
public static void main(String[] args) throws IOException {
File file = new File("src\\main\\java\\json.txt");
FileInputStream value = new FileInputStream(file);
ObjectMapper object = new ObjectMapper();
JsonNode jsonNode = object.readTree(value);
for (int i=0;i<jsonNode.size();i++){
System.out.println(jsonNode.get(i).findValue("sender").textValue());
}
}
Related
The question is quite simple:
From this:
{
"categoryId":"some_id",
"properties": {
"id": "braja_de_nana",
"displayName": "test",
"longDescription": "<p>TESTE</p>",
"active": true,
"attributes": [
{
"name": "made",
"value": "THIS_BECOMES_A_NODE_VALUE",
"property": "THIS_BECOMES_A_NODE_NAME"
},
{
"name": "made",
"value": "THIS_BECOMES_A_NODE_VALUE_2",
"property": "THIS_BECOMES_A_NODE_NAME_2"
}
]
}
}
UPDATE
This should be the result:
It means that every array element of 'attributes' should become a new root node.
set 'property' from 'attributes' as the object node name.
set 'value' from 'attributes' as the object node value.
{
"categoryId":"some_id",
"THIS_BECOMES_A_NODE_VALUE":"THIS_BECOMES_A_NODE_NAME",
"THIS_BECOMES_A_NODE_NAME_2":"THIS_BECOMES_A_NODE_VALUE_2"
"properties": {
"id": "braja_de_nana",
"displayName": "test",
"longDescription": "<p>TESTE</p>",
"active": true
}
}
This is a challenge for me.
I can set new nodes into the root node.
Already got a map from 'attributes' and then tried to iterate them with forEach in order to put the result into one single node, but instead as shown I have to take the 'property' set it to the object name´s key, then get the value and set to its value.
UPDATE 2
#Override
public String toOccProductDTO(ProcessProductDTO processProductDTO) throws JsonProcessingException {
OccProductDTO occProductDTO = OccProductDTO.builder()
.categoryId(processProductDTO.getCategoryId())
.productType(processProductDTO.getCategoryId())
.properties(toOccProductPropertiesDTO(processProductDTO))
.build();
toOccProductPropertiesDTO(processProductDTO);
String tree = mapper.writeValueAsString(occProductDTO);
JsonNode root = mapper.readTree(tree);
JsonNode attributesNodeArray = ((ObjectNode) root.get("properties"))
.remove("p_specs");
Iterator<JsonNode> arrayNodes = attributesNodeArray.iterator();
while (arrayNodes.hasNext()) {
JsonNode node = arrayNodes.next();
root = ((ObjectNode)root).set(node.get("value").asText(), node.get("property"));
}
System.out.println(root.toPrettyString());
return null;
}
I got an: arrayNodes: Collection$EmptyIterator at that line.
Am I doing something wrong?
If you are trying to the attributes to the root node, you can remove that node and add its fields to the root.
The "attributes" node is an array with length 1, so you have to get the first element of the array to get the attribute fields.
ObjectMapper mapper = new ObjectMapper();
JsonNode root = mapper.readTree(json);
JsonNode attributesNodeArray = ((ObjectNode) root.get("properties"))
.remove("attributes");
JsonNode attributesNode = attributesNodeArray.get(0);
Iterator<String> fieldNames = attributesNode.fieldNames();
while (fieldNames.hasNext()) {
String name = fieldNames.next();
root = ((ObjectNode)root).set(name, attributesNode.get(name));
}
System.out.println(root.toPrettyString());
Output:
{
"categoryId" : "some_id",
"properties" : {
"id" : "braja_de_nana",
"displayName" : "test",
"longDescription" : "<p>TESTE</p>",
"active" : true
},
"name" : "made",
"value" : "some value",
"property" : "some_value"
}
UPDATE
For the updated question, you can do the following:
ObjectMapper mapper = new ObjectMapper();
JsonNode root = mapper.readTree(json);
JsonNode attributesNodeArray = ((ObjectNode) root.get("properties"))
.remove("attributes");
Iterator<JsonNode> arrayNodes = attributesNodeArray.iterator();
while (arrayNodes.hasNext()) {
JsonNode node = arrayNodes.next();
root = ((ObjectNode)root).set(node.get("value").asText(), node.get("property"));
}
System.out.println(root.toPrettyString());
Output:
{
"categoryId" : "some_id",
"properties" : {
"id" : "braja_de_nana",
"displayName" : "test",
"longDescription" : "<p>TESTE</p>",
"active" : true
},
"THIS_BECOMES_A_NODE_VALUE" : "THIS_BECOMES_A_NODE_NAME",
"THIS_BECOMES_A_NODE_VALUE_2" : "THIS_BECOMES_A_NODE_NAME_2"
}
Maybe it was more complex than expected.
It turns out that I solved the problem with Oboe´s help. Although he missed some points through his implementation plus some changes I could achieve the goal.
//Converts the parsed objects into Json String
String tree = mapper.writeValueAsString(occProductDTO);
//Reads the json string to JsonNode in order to manipulate it
JsonNode root = mapper.readTree(tree);
//Sets the chosen node where the new nodes should be created
JsonNode properties = root.path("properties");
//maps the two attribs needed
Map<String, String> attribs = processProductDTO.getProductDTO().getAttributes().stream()
.collect(Collectors.toMap(AttributeDTO::getProperty, AttributeDTO::getValue));
//Converts each attrib into a String list
List<String> props = attribs.entrySet().stream()
.sorted(Comparator.comparing(Map.Entry<String, String>::getValue).reversed())
.map(Map.Entry::getKey)
.collect(Collectors.toList());
List<String> names = attribs.entrySet()
.stream() .sorted(Comparator.comparing(Map.Entry<String,String>::getValue).reversed())
.map(Map.Entry::getValue)
.collect(Collectors.toList());
//iterates over the two lists adding the attribs to their corresponding position
Iterator<String> arrayNodes = props.listIterator();
Iterator<String> arrayNodes2 = names.listIterator();
while (arrayNodes.hasNext()) {
String node = arrayNodes.next();
String node2 = arrayNodes2.next();
properties = ((ObjectNode)properties).put(node, node2);
}
return mapper.writeValueAsString(root);
}
In the end, instead of passing a java object via #Post, I´m passing a json String by using "consumes = application/json, produces = "application.json"
That´s it!
Maybe it could be achieved and better implemented with java 8 stream, but for now it works.
Suggestion to improve the code are welcome!
I have a following response from a HTTP call which looks like this...
[{"id": 1, "name" : abc, "above50" : true} , {"id": 2, "name" : "xyc", "above50" : false, "kids" : "yes"} ]
I need to iterate through this list and find if there is a key called kids and if there is the key kids, i need to store the value . How do i do it in java?
First you need to parse the json string - it's a list of objects. If you don't have classes to match those objects, by default they can be represented as Map<String, Object>. Then you need to iterate the list, and for every object in it, you have to iterate the entries in the object. If the key matches, store it.
//parse json string with whatever parser you like
List<Map<String, Object>> list = ...;
//iterate every object in the list
for (Map<String, Object> map : list) {
//iterate every entry in the object
for (Map.Entry<String, Object> entry : map.entrySet()) {
if (entry.getKey().equals("kids")) {
//you can store the key and the value however you want/need
System.out.println(entry.getKey() + " -> " + entry.getValue());
}
}
}
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
-------------------------------------------
#Test
public void test04() throws IOException {
final String preString = "[{\"id\": 1, \"name\" : \"abc\", \"above50\" : true} , {\"id\": 2, \"name\" : \"xyc\", \"above50\" : false, \"kids\" : \"yes\"} ]";
final ObjectMapper objectMapper = new ObjectMapper();
final JsonNode arrayNode = objectMapper.readTree(preString);
if (arrayNode.isArray()) {
for (JsonNode it : arrayNode) {
final JsonNode kids = it.get("kids");
if (kids != null) {
//TODO: Storage this value by you want
System.out.println(kids.asText());
}
}
}
}
You can use JSONObject or JSONArray
String message = ""list" : [{"id": 1, "name" : abc, "above50" : true} , {"id": 2, "name" : "xyc", "above50" : false, "kids" : "yes"} ]";
JSONObject jsonObject = new JSONObject(message);
JSONArray array = jsonObject.getJsonArray("list");
//so now inside the jsonArray there is 2 jsonObject
//then you can parse the jsonArray and check if there is
//a jsonObject that have "kids" like jsonObject.get("kids") != null
// or jsonObject.getString("kids") != null
I am testing REST api. Below is my JSON response.
[ {
"id" : 1,
"name" : "Chattagram"
}, {
"id" : 2,
"name" : "Rajshahi"
}, {
"id" : 3,
"name" : "Khulna"
}, {
"id" : 4,
"name" : "Barisal"
}, {
"id" : 5,
"name" : "Sylhet"
}, {
"id" : 6,
"name" : "Dhaka"
}, {
"id" : 7,
"name" : "Rangpur"
}, {
"id" : 8,
"name" : "Mymensingh"
} ]
I have converted the list into a List of HashMap. I am able to access the name field but when trying to get the id, below exception is happening. I am not understanding why this exception, cause the key is correct. I have given the debug console image, can any one tell me what is the issue and how i can access the id?
java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
Source Code :
#Test
public void getEmployees() throws IOException {
final String uri = "http://localhost:8080/public/geocode/divisions";
RestTemplate restTemplate = new RestTemplate();
String result = restTemplate.getForObject(uri, String.class);
restTemplate.getForObject(uri, String.class);
System.out.println(result);
ObjectMapper mapper = new ObjectMapper();
List<Map<String, String>> list = new ArrayList<>();
list = mapper.readValue(result, List.class);
System.out.println(list.get(0));//--------------out put : {id=1, name=Chattagram}
Map<String, String> m = list.get(0);
System.out.println("id : " + m.get("id")); //------------problem line
System.out.println("Name : " + m.get("name")); //------------working fine
System.out.println(list);
}
After setting a break point in the problem line, debug variables :
Try
System.out.println("id : " + Integer.toString(m.get("id"));
I have a schema like this:
{
"type" : "object",
"$schema" : "http://json-schema.org/draft-03/schema#",
"id" : "urn:jsonschema:com:vlashel:dto:UserDto",
"description" : "this is the top description",
"title" : "this is the top title",
"properties" : {
"number" : {
"type" : "integer"
"required" : true
},
"password" : {
"type" : "string"
"required" : true
}
}
I have the following code that converts this shcema draft 3 to draft 4 by removing "required", I want to collect nodes property names that have 'requred' in them. How do I do that? I don't see methods for this..
JsonNode jsonNode = jsonNodeIterator.next();
ObjectNode element;
if (jsonNode instanceof ObjectNode) {
element = (ObjectNode) jsonNode;
element.remove("required");
String propertyName = element.getPropertyName(); //I'm looking for this kind of method.
Thanks!
You can get all of the nodes that have that property by using the List<JsonNode> findParents(String fieldName), which does that this for you. From the docs:
Method for finding a JSON Object that contains specified field, within
this node or its descendants. If no matching field is found in this
node or its descendants, returns null.
I made a quick example but had to add a few characters to the JSON blob you posted as it is missing some commas and a bracket and can't be read by the ObjectMapper. It is as simple as this:
JsonNode root = mapper.readTree(SCHEMA);
List<JsonNode> required = root.findParents("required");
for (JsonNode node: required) {
Object prettyOutput = mapper.readValue(node, Object.class);
System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(prettyOutput));
}
Output:
{
"type" : "integer",
"required" : true
}
{
"type" : "string",
"required" : true
}
I'm a little stuck with the following problem.
From a MongoDB I get this JSON string, and I want to get all the valuas of 'photo' and put them in an ArrayList. I've found some examples but they aren't working for me. I'm using GSON but a JSON solution will also be fine.
Return from MongoDB:
[ { "url" : "/photos/avatar-1.jpg" , "photo" : "avatar-1.jpg" , "description" : "test 1"} , { "url" : "/photos/avatar-2.jpg" , "photo" : "avatar-2.jpg" , "description" : "test 2"} , { "url" : "/photos/avatar-3.jpg" , "photo" : "avatar-3.jpg" , "description" : "test 3"} , { "url" : "/photos/avatar-4.jpg" , "photo" : "avatar-4.jpg" , "description" : "test 4"}]
Putting into an ArrayList isn't the problem, but getting the 'photo' value. If someone could give me an example on how to loop through the 4 arrays and a System.out.println of the 'photo' value, that would be great!!
Thanks!
jsonString =[...];
Gson gson = new Gson();
PhotoDTO[] photos = gson.fromJson(jsonString, PhotoDTO[].class);
for(PhotoDTO photo : photos){
System.out.println("photo -> " + photo.getPhoto());
}
PhotoDTO Class Definition
class PhotoDTO
{
String url;
String photo;
String description;
// setters & getters methods
}
Using the excellent json-simple library, you should be able to do something like this:
String json = "[{\"photo\": \"1.png\"}, {\"photo\": \"2.png\"}, " +
"{\"photo\": \"3.png\"}, {\"photo\": \"4.png\"}]";
JSONArray photos = (JSONArray)JSONValue.parse(json);
for (int index = 0; index < photos.size(); index++) {
JSONObject photoObj = (JSONObject)photos.get(index);
System.out.println(photoObj.get("photo"));
}
Obviously replace the literal JSON text with the data that you're fetching from your database.
Another option without creating DTOs, using Gson class StringMap:
String json = "[ { \"url\" : \"/photos/avatar-1.jpg\" , \"photo\" : \"avatar-1.jpg\" , \"description\" : \"test 1\"} , { \"url\" : \"/photos/avatar-2.jpg\" , \"photo\" : \"avatar-2.jpg\" , \"description\" : \"test 2\"} , { \"url\" : \"/photos/avatar-3.jpg\" , \"photo\" : \"avatar-3.jpg\" , \"description\" : \"test 3\"} , { \"url\" : \"/photos/avatar-4.jpg\" , \"photo\" : \"avatar-4.jpg\" , \"description\" : \"test 4\"}]";
Gson gson = new Gson();
Type type = new TypeToken<List<StringMap<String>>>(){}.getType();
List<StringMap<String>> stringMaps = gson.fromJson(json, type);
for (StringMap<String> map:stringMaps) {
System.out.println(map.get("photo"));
}
EDIT
Imports:
import com.google.gson.Gson;
import com.google.gson.internal.StringMap;
import com.google.gson.reflect.TypeToken;