Search code examples
jsonjsonpathjayway

Using JsonPath - how can I get the subset of properties of one object based on field names in another object (Jayway)


Given one object ("includeFields") specifying which properties from another object ("toBeFiltered") should be extracted into a new resulting object:

{
  "toBeFiltered": {
    "a": "AAA",    
    "b": "BBB",    // Excluded from the result as "includeFields" doesn't include "b"
    "c": "CCC"     
  },
  "includeFields": {
    "a": true,
    "c": true 
  } 
}

When: Some JsonPath expression checking which field names in "toBeFiltered" are also present in "includeFields"

Then:

{
  "a": "AAA",
  "c": "CCC"
}

Is it possible to do the with JsonPath and if so - what would the JsonPath (Jayway) expression look like?


Solution

  • JSON Path isn't going to perform transformations like this. It's only a query syntax: it'll only tell you if and where items are.

    There are other syntaxes which can do that, like JMESPath, which is largely based on JSON Path.