Jsonpath for nested JSON objects

Datacrawler picture Datacrawler · Dec 4, 2017 · Viewed 12.1k times · Source

I have a JSON with nested fields:

  [
    {
    "Platform Parent Dato Id": "23768",
    "Platform Dato Id": "24138",
    "Platform Dato Name": "Random Europe",
    "Platform mission Id": "111112",
    "Platform submission Id": "638687",
    "Platform submission Flight Id": "863524",
    "Start Date": "2017-12-01",
    "End Date": "2017-12-02",
    "Platform Compensation": 109.0909,
    "Total Value": 909.0909,
    "Goal": "200000.0000",
    "Value Information": {
      "Platform Compensation": [
        {
          "Platform mission Id": "111112",
          "Platform submission Id": "638687",
          "Platform submission Flight Id": "863524",
          "Value Rate": "14.0000",
          "Value": 109.0909
        }
      ]
    }
  },
  {
    "Platform Parent Dato Id": "23768",
    "Platform Dato Id": "24138",
    "Platform Dato Name": "Random Europe",
    "Platform mission Id": "111113",
    "Platform submission Id": "638687",
    "Platform submission Flight Id": "863524",
    "Start Date": "2017-12-01",
    "End Date": "2017-12-02",
    "Platform Compensation": 109.0909,
    "Total Value": 909.0909,
    "Goal": "200000.0000",
    "Value Information": {
      "Platform Compensation": [
        {
          "Platform mission Id": "111113",
          "Platform submission Id": "638687",
          "Platform submission Flight Id": "863524",
          "Value Rate": "12.0000",
          "Value": 109.0909
        }
      ]
    }
  }
  ]

and I am using a JSONPATH in order to get the Value Rate from the Value Information nest.

I have pasted my JSON text in this website: http://jsonpath.com/ and after using this line:

$[*].['Platform Compensation'].['Value Rate']

I am getting this:

enter image description here

and after using this line:

$.['Value Information'].['Platform Compensation'].['Platform mission Id']

I am getting this:

enter image description here

What I am trying to return (output) is the following:

enter image description here

but I cannot find the right syntax to combine those two in one line and return both with one JSONPATH query.

Answer

glytching picture glytching · Dec 4, 2017

jsonpath can be used to select values for given expressions and - in some implementations - for customised predicates but it does not support projections.

You can use jsonpath to filter your given JSON. For example:

  • Return an array containing all of the Platform Compensation values:

    $.['Value Information'].['Platform Compensation'].['Platform mission Id']
    
  • Return an array containing all of the Platform mission Id values:

    $.['Value Information'].['Platform Compensation']
    

But you cannot use jsonpath to a read a sub-set of keys and values. To read a sub-set of keys and values you would need to use JSON de/serialisation library. Commonly used libraries - in the Java world - are such as Jackson and Gson.

Here's an example using Jackson:

String json = "...";

ObjectMapper mapper = new ObjectMapper();
Map<String, Object> asMap = mapper.readValue(json, Map.class);

Map<String, Object> transformed = new HashMap<>();
transformed.put("Platform mission Id", asMap.get("Platform mission Id"));
transformed.put("Value Rate", asMap.get("Value Rate"));

String result = mapper.writeValueAsString(transformed);