Selecting a subset of JSON properties and values using JSONPath

jsonpath multiple fields
generate jsonpath from json
jsonpath concatenate
jsonpath methods
json query language
jsonpath examples
convert jsonpath to json object
create json object using jsonpath

Given a JSON like:

{
  "a":1,
  "b":2,
  "c":3,
  "d":4,
  "e":5
}

How can I select out b, d and e to get the following JSON?

{
  "b":2,
  "d":4,
  "e":5
}

I want a JSON object and NOT only 2, 4 and 5 values?

This is what I'm trying with and failing:

$.[b,d,e]

JSONPath is not suitable for what you are trying to achieve: JSONPath is designed to select values and not key-value pairs. What you want could be achieved with Jackson or any JSON parser for Java.

If you want to go for Jackson here's the code that will do the trick:

String json = "{\"a\":1,\"b\":2,\"c\":3,\"d\":4,\"e\":5}";

ObjectMapper mapper = new ObjectMapper();
JsonNode tree = mapper.readTree(json);

ObjectNode node = mapper.createObjectNode();
node.set("b", tree.get("b"));
node.set("d", tree.get("d"));
node.set("e", tree.get("e"));

String result = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(node);

Selecting a subset of JSON properties and values using JSONPath, How can I select out b , d and e to get the following JSON? { "b":2, "d":4, "e":5 }. I want a JSON object and NOT only 2 , 4 and 5 values? This is what I'm trying with​  JSON Path is an XPath like query language for JSON (JavaScript Object Notation) that enables you to select nodes in a JSON document. The Retrieve Attributes with JSON Path filter enables you to retrieve specified message attributes from a JSON message using JSON Path expressions.

Your json path is correct, while json it self is not. It should be:

{
"a":1,
"b":2,
"c":3,
"d":4,
"e":5
}

BTW there is good online testing resources for these purposes: http://jsonpath.com/

Retrieve attributes with JSON path, JSON Path is an XPath like query language for JSON (JavaScript Object Notation​) that enables you to select nodes in a JSON document. String : Enter this value when using this filter to extract a single value and store it in an API Gateway  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. This comment has been minimized.

Elements must be in single quotes.

$.['b','d','e']

Works fine for JsonPath from com.jayway.jsonpath:json-path:2.4.0

JSONPath Reference, In ReadyAPI, you can apply JSONPath expressions to refer specific objects or Remove n2 from the expression to select array items from n1 to the end of an Note: All of the JSONPath expressions (including property names and values) are Apply filters to JSON arrays and objects to get a subset of items that match the  ..property: Recursive descent: Searches for the specified property name recursively and returns an array of all values with this property name. Always returns a list, even if just one property is found. * Wildcard selects all elements in an object or an array, regardless of their names or indexes.

You need to modify your JSON to (as said by Andremoniy)

{
"a":1,
"b":2,
"c":3,
"d":4,
"e":5
}

and to select b,d,e use this

$.b,d,e

Extracting subset of fields from JSON object · Issue #86 · dchester , 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  In Javascript and JSON it is the native array operator. [,] Union operator in XPath results in a combination of node sets. JSONPath allows alternate names or array indices as a set. [start:end:step] array slice operator borrowed from ES4.?() applies a filter (script) expression. script expression, using the underlying script engine.

What is JSONPath and How to query JSON using JSONPath?, How to Get child node from JSON Object with JSONPath? expressions to traverse through a JSON document to reach to a subset of the JSON. You can also refer to the last item by giving a positive value to the index. Array slice operator is wonderful operator to extract selected items from Json. Taking  JSONPath and Query JSON using JSONPath. One of the most important advantage of JSON is that it is a light weight format that can be used to interchange data between computers and processes. JSON, like XML, is a format to provide structure to the data. If you are not familiar with JSON, please go through this tutorial first of Introduction to JSON.

JSON extraction using JSONPath, Additionally, scripts can use two new JSONPath Message properties via This value retrieval method can also be manually selected where  In the JSONPath Expression edit box, specify the JSONPath expression to point at the element you want to compare the result with, or use the toolbar controls to create the expression automatically. To use the toolbar controls, run the request you want to validate at least once. Click the image to enlarge it. Click to call the Select JSON dialog.

How to write JSONPath expressions, JSONPath uses JSONPath expressions to select elements from a JSON structure​. Let us take a min(), Provides the min value of an array of numbers, Double. JSONPath is a query language for JSON that allows you to refer to a JSON object structure in the same way as XPath expressions do for XML documents. In ReadyAPI, you can apply JSONPath expressions to refer specific objects or elements in requests and responses, which is useful for assertions or property expansions that work with JSON objects.

Comments
  • JSONPath is not suitable for that.
  • Possible duplicate of Extracting a subset of attributes with JSONPath