JSONPath is a query language for JSON with features similar to XPath for XML. JSONPath is used for selecting and extracting a sub-section from the JSON document.
1. JSONPath Dependencies
To use JSONPath, we will need to include its dependency and then use it.
Language | Dependency |
---|---|
JavaScript | |
Node | npm install JSONPath |
PHP | |
Python | pip install jsonpath-rw |
Java |
2. JSONPath Syntax
A JsonPath expression begins with the dollar sign ($
) character, which refers to the root element of a query. The dollar sign is followed by a sequence of child elements, which are separated via dot (code) notation or via the square brackets (code).
The important JSONPath syntax rules are:
$
symbol refers to the root object or element.@
symbol refers to the current object or element..
operator is the dot-child operator, which you use to denote a child element of the current element.[ ]
is the subscript operator, which you use to denote a child element of the current element (by name or index).*
operator is a wildcard, returning all objects or elements regardless of their names.,
operator is the union operator, which returns the union of the children or indexes indicated.:
operator is the array slice operator, so you can slice collections using the syntax[start:end:step]
to return a subcollection of a collection.( )
operator lets you pass a script expression in the underlying implementation’s script language. It’s not supported by every implementation of JSONPath, however.? ( )
to query all items that meet a certain criteria.
3. JSONPath Expression Examples
Below given are few examples of JSONPath.
$.store.book[0].title
$.store.book[*].title
$..book[3]
//or using brackets
$['store']['book'][0].['title']
$['store']['book'][*].['title']
$..['book'][3]
$.store.book[?(@.price < 10)].title
4. JSONPath Example using JavaScript
We have the following JSON document. We will apply the JSONPath expressions to it.
{
"store":
{
"book": [
{
"category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95
},
{
"category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99
},
{
"category": "fiction",
"author": "Herman Melville",
"title": "Moby Dick",
"isbn": "0-553-21311-3",
"price": 8.99
},
{
"category": "fiction",
"author": "J. R. R. Tolkien",
"title": "The Lord of the Rings",
"isbn": "0-395-19395-8",
"price": 22.99
}
],
"bicycle": {
"color": "red",
"price": 19.95
}
}
}
Example 1
Using JSONPath to find the names of all authors.
var response = jsonPath(store , "$..author").toJSONString();
Program output:
[
"Nigel Rees",
"Evelyn Waugh",
"Herman Melville",
"J. R. R. Tolkien"
]
Example 2
Using JSONPath to find the details for book number 4. The array index is zero-based.
var response = jsonPath(store , "$..book[3]").toJSONString();
Program output:
[
{
"category":"fiction",
"author":"J. R. R. Tolkien",
"title":"The Lord of the Rings",
"isbn":"0-395-19395-8",
"price":22.99
}
]
how can i get the string value from the array
input ::
{“characteristics”: [
{
“name”: “ICCID”,
“valueType”: “string”,
“value”: “8925102410460008907”
},
{
“name”: “IMSI”,
“valueType”: “string”,
“value”: “636024100008907”
}
]
}
output ::
“8925102410460008907”
but its coming like when i tried with $.characteristics[?(@.name==”ICCID”)].value
[
“8925102410460008907”
]
Can I match a top level attribute value
{
“category”: “reference”,
“author”: “Nigel Rees”,
“title”: “Sayings of the Century”,
“price”: 8.95
}
Need to verify, “category”: “reference”, is present in json or not.
I tried $.[?(category == ‘reference’)]
Can we provide multiple filters like
Book [(category = fiction and
Author = Evelyn Waugh) and (category= reference and author= Nigel Rees)]
Yes, it is possible. I quickly tried it on this tool.
$..book[?((@.category=='fiction' && @.author=='Evelyn Waugh') || (@.category=='reference' && @.author=='Nigel Rees'))]
This looks like the original passport JSON data. Please remove it as it violates the person’s privacy.
Can i use jsonpath in url encoding?
From the above data, how can I get person details who lived in Nashville for example. I am expecting the following result
Thank you!
$.[?(@.city=="Nashville")]^^^^
Looks like this is working fine.
Can i transform the json string into another format. I want the JSON string to be transform into array of array where property name in book becomes the column names. ?
Can i write regular expression to match the string in javascript. i saw it working in jay way which is for java but in javascript its not working.
Yes,
string.match(regex)
takes regular expression in javascript.How do I write back to json file using JSONPath ?
nshah I don’t think it is possible to write json elements using JSONPath. Otherwise, it would surely be listed on this page.
How can I check if members contain ‘allUsers’
Create XPath expression with help of information given in this xpath example.
$•[?(@.members==”allUsers”]
How can i append two string response from json using jsonpath ?
Ex. JsonResponse
I just want to append name and surname like
SITA MAHA LAKSHMI RAMADUGULA
.When i tried using
$['passport']['page_1']['name','surname']
its giving me value :that i don’t want. Any idea about it ??
$.passport.page_1.(name & ' ' & surname)
Result : “SITA MAHA LAKSHMI RAMADUGULA”