jsonpath icon indicating copy to clipboard operation
jsonpath copied to clipboard

Extracting subset of fields from JSON object

Open narciero opened this issue 7 years ago • 6 comments
trafficstars

A feature that the Jayway JsonPath parser has that is very useful is being able to select a subset of fields within a JSON object.

Using the Goessner example here: http://jsonpath.herokuapp.com/, the jsonpath:

$.store.book[*].['category', 'author']

would return all books with only the category and author fields:

[
   {
      "category" : "reference",
      "author" : "Nigel Rees"
   },
  ...
]

It would be great if this library supported this as well. Thanks and great work!

narciero avatar Jan 08 '18 15:01 narciero

I totally agree.

Json Path is a powerful tool but the way this implementation displays the subset fields is not quite very useful.

+1 for this feature.

Thanks a lot for the work already put in!

adrianthedev avatar Jan 27 '18 04:01 adrianthedev

It does support that, just the parser won't allow spaces

"$[a,b]" with no spaces should work

sdawood avatar Sep 19 '18 08:09 sdawood

@sdawood I tried your syntax and the results are returned as follows:

[
  "1st_value_for_a",
  "1st_value_for_b"
  "2nd_value_for_a",
  "2nd_value_for_b"
  "3rd_value_for_a",
  "3rd_value_for_b"
]

Instead of each pair separated in its own object.

Gloix avatar Oct 15 '19 18:10 Gloix

The no-spaces is a parser Grammer limitation that can be easily fixed in the future. The behavior of the AST evaluation code (the handler function) is responsible for the data shape, also should be revised.

sdawood avatar Oct 16 '19 00:10 sdawood

Just FYI, the Jayway JsonPath feature to extract a subset of members from a json object is actually a bug in the Jayway JsonPath parser, see [,] is intersection, not a union operator for field names.

danielaparker avatar Dec 10 '19 12:12 danielaparker

Too bad that very useful feature is threated like a bug )

NotEvenFlinched avatar Jan 08 '20 13:01 NotEvenFlinched