Home > OS >  Remove outermost node from the JSON payload
Remove outermost node from the JSON payload

Time:10-22

I need to remove the outermost element(ns0:TableData) from the below JSON paylaod.

{
    "ns0:TableData": {
        "descr": 111,
        "note": 11,
        "kpar": 1111,
        "karr": 111,
        "xmlns:ns0": "urn:it:alia:inaz",
        "codice": 1,
        "dend": 1111,
        "anz_app_a": 1,
        "dini": 11
    }
}

I am using the below code to covert the incoming XML to JSON

String inputData = IOUtils.toString(inputstream);
System.out.println(inputData);
JSONObject xmlJSONObj = XML.toJSONObject(inputData);
String jsonPrettyPrintString = xmlJSONObj.toString(PRETTY_PRINT_INDENT_FACTOR);
System.out.println(jsonPrettyPrintString);

CodePudding user response:

In XPath 3.1, use json-doc('input.json')?*

CodePudding user response:

Try this example

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import lombok.Data;
import lombok.ToString;

public class Main {
    @Data
    @ToString
    private static class All {
        @JsonProperty("descr")
        int descr;
        int note;
        int kpar;
        int karr;
        @JsonProperty("xmlns:ns0")
        String xmlnsNs0;
        int codice;
        int dend;
        @JsonProperty("anz_app_a")
        int anzAppA;
        int dini;
    }

    public static void main(String[] args) throws JsonMappingException, JsonProcessingException {
        String json = "{\n" //
                  "    \"ns0:TableData\": {\n" //
                  "        \"descr\": 111,\n" //
                  "        \"note\": 11,\n" //
                  "        \"kpar\": 1111,\n" //
                  "        \"karr\": 111,\n" //
                  "        \"xmlns:ns0\": \"urn:it:alia:inaz\",\n" //
                  "        \"codice\": 1,\n" //
                  "        \"dend\": 1111,\n" //
                  "        \"anz_app_a\": 1,\n" //
                  "        \"dini\": 11\n" //
                  "    }\n" //
                  "}\n" 
                  "";
        ObjectMapper obj = new ObjectMapper();
        JsonNode jstree = obj.readTree(json);
        JsonNode data = jstree.get("ns0:TableData");
        All a = obj.readValue(data.toString(), All.class);
        System.out.println(a);
    }
}
  • Related