Home > front end >  Jackson2 save ObjectNode as String
Jackson2 save ObjectNode as String

Time:10-03

i have example of string json like below :

{
   "eventName":"PARTY_COLLECTED",
   "payload":{
      "vendor":"IBM",
      "appVersion":"0.0.1",
      "deviceTime":"2021-09-23T04:34:56.086Z",
      "app":"ibm",
      "platform":"windows",
      "data":{
         "album":[
            {
               "hidden":false,
               "mediaType":"Image",
               "locationLongitude":-1.80317833333334,
               "modificationDate":"2015-09-02 20:02:27  0000",
               "creationDate":"2011-03-13 00:17:25  0000",
               "pixelHeight":1010,
               "locationLatitude":138.0374445,
               "duration":0,
               "favorite":false,
               "pixelWidth":8588,
               "localIdentifier":"104449A1-4G1A-4AB2-B120-B0AD4A8E5555/L0/001"
            }
         ],
         "contacs":[
            
         ],
         "HistoryDevice":[
            {
               "another_data1":"data1",
               "another_data1":"data1",
               "another_data1":"data1",
               "another_data1":"data1",
               "another_data1":"data1"
            }
         ]
      }
   }
}

i want to ask, how can i get all value inside of "data" as String using Java Jackson2 library?
for example, i have variable call hugeString, so the value of hugeString should be like this:

hugeString="{
         "album":[
            {
               "hidden":false,
               "mediaType":"Image",
               "locationLongitude":-1.80317833333334,
               "modificationDate":"2015-09-02 20:02:27  0000",
               "creationDate":"2011-03-13 00:17:25  0000",
               "pixelHeight":1010,
               "locationLatitude":138.0374445,
               "duration":0,
               "favorite":false,
               "pixelWidth":8588,
               "localIdentifier":"104449A1-4G1A-4AB2-B120-B0AD4A8E5555/L0/001"
            }
         ],
         "contacs":[

         ],
         "HistoryDevice":[
            {
               "another_data1":"data1",
               "another_data1":"data1",
               "another_data1":"data1",
               "another_data1":"data1",
               "another_data1":"data1"
            }
         ]
      }"

i already try using this code below:

object StreamData {
  case class StreamDatas_rows(
                          data:String
                        )
  def jsonToRow(json_object:ObjectNode):StreamDatas_rows={
    def getStringOrNull(jsonNode:JsonNode):String=if(jsonNode==null||jsonNode.isNull) "" else jsonNode.asText()
    StreamDatas_rows(
       json_object.path("payload").get("data").asText()
    )
  }
}

and then call in my main job:

val ss = ds.map(s=>s)
ss.print()

but no luck, always get an empty variable.

CodePudding user response:

Below code is the implementation in Java.

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.LinkedHashMap;

public class SO_Jackson {

    private static void parseJson(String jsonData) throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        JsonNode jsonNode = objectMapper.readTree(jsonData);
        JsonNode node = jsonNode.at("/payload/data");
        String subData = objectMapper.writeValueAsString(node);
        System.out.println(subData);
    }
    public static void main(String[] args) throws JsonProcessingException {
        String jsonData = "{\"eventName\":\"PARTY_COLLECTED\",\"payload\":{\"vendor\":\"IBM\",\"appVersion\":\"0.0.1\",\"deviceTime\":\"2021-09-23T04:34:56.086Z\",\"app\":\"ibm\",\"platform\":\"windows\",\"data\":{\"album\":[{\"hidden\":false,\"mediaType\":\"Image\",\"locationLongitude\":-1.80317833333334,\"modificationDate\":\"2015-09-02 20:02:27  0000\",\"creationDate\":\"2011-03-13 00:17:25  0000\",\"pixelHeight\":1010,\"locationLatitude\":138.0374445,\"duration\":0,\"favorite\":false,\"pixelWidth\":8588,\"localIdentifier\":\"104449A1-4G1A-4AB2-B120-B0AD4A8E5555/L0/001\"}],\"contacs\":[],\"HistoryDevice\":[{\"another_data1\":\"data1\",\"another_data1\":\"data1\",\"another_data1\":\"data1\",\"another_data1\":\"data1\",\"another_data1\":\"data1\"}]}}}";
        parseJson(jsonData);
    }
}

CodePudding user response:

An Option would be to use org.json.JSONObject and do something like this:

JSONObject jsonResult = new JSONObject(dataString);
String hugeString = jsonResult.getJSONObject("payload").getString("data");
  • Related