dchester / jsonpath

Query and manipulate JavaScript objects with JSONPath expressions. Robust JSONPath engine for Node.js.
MIT License
1.32k stars 216 forks source link

Extracting subset of fields from JSON object #86

Open narciero opened 6 years ago

narciero commented 6 years ago

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!

adrianthedev commented 6 years ago

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!

sdawood commented 5 years ago

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

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

Gloix commented 4 years ago

@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.

sdawood commented 4 years ago

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.

danielaparker commented 4 years ago

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.

NotEvenFlinched commented 4 years ago

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