9

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.

Datacrawler
  • 2,780
  • 8
  • 46
  • 100
  • $.['Value Information'].['Platform Compensation'].* , [this](https://github.com/json-path/JsonPath) may help you – Onkar Dec 04 '17 at 13:36
  • @Onkar That returns all the fields of the nested object. Which is fine. But how can I also get (for example) the "Platform Parent Dato Id"? – Datacrawler Dec 04 '17 at 14:17

2 Answers2

3

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);
glytching
  • 44,936
  • 9
  • 114
  • 120
3

Jayway implementation let you do that. The jsonpath would be $.[*]['Value Information']['Platform Compensation'][*]['Platform mission Id', 'Value Rate']

You can try it in this website http://jsonpath.herokuapp.com/

Pompelmo
  • 31
  • 2