json-path<\/a><\/div><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n2. JSONPath Syntax<\/h2>\n\n\n\n
A JsonPath expression begins with the dollar sign ($<\/code>) 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).<\/p>\n\n\n\nThe important JSONPath syntax rules<\/em> are:<\/p>\n\n\n\n $ <\/code> symbol refers to the root object<\/strong> or element.<\/li> @ <\/code> symbol refers to the current object<\/strong> or element.<\/li> . <\/code> operator is the dot-child operator, which you use to denote a child element<\/strong> of the current element.<\/li> [ ] <\/code> is the subscript operator, which you use to denote a child element of the current element (by name or index)<\/strong>.<\/li> * <\/code> operator is a wildcard, returning all objects<\/strong> or elements regardless of their names.<\/li> , <\/code> operator is the union operator, which returns the union of the children<\/strong> or indexes indicated.<\/li> : <\/code> operator is the array slice operator<\/strong>, so you can slice collections using the syntax [start:end:step]<\/code> to return a subcollection of a collection.<\/li> ( ) <\/code> operator lets you pass a script expression<\/strong> in the underlying implementation’s script language. It’s not supported by every implementation of JSONPath, however.<\/li> ? ( )<\/code> to query all items that meet a certain criteria<\/em>.<\/li><\/ul>\n\n\n\n3. JSONPath Expression Examples<\/h2>\n\n\n\n
Below given are few examples of JSONPath.<\/p>\n\n\n\n
$.store.book[0].title\n$.store.book[*].title\n$..book[3]\n\n\/\/or using brackets\n\n$['store']['book'][0].['title']\n$['store']['book'][*].['title']\n$..['book'][3]\n\n$.store.book[?(@.price < 10)].title<\/code><\/pre>\n\n\n\n4. JSONPath Example using JavaScript<\/h2>\n\n\n\n
We have the following JSON document. We will apply the JSONPath expressions to it.<\/p>\n\n\n\n
{ \n\t\"store\": \n\t{\n\t\t\"book\": [\n\t\t\t{ \n\t\t\t\t\"category\": \"reference\",\n\t\t\t\t\"author\": \"Nigel Rees\",\n\t\t\t\t\"title\": \"Sayings of the Century\",\n\t\t\t\t\"price\": 8.95\n\t\t\t},\n\t\t\t{ \n\t\t\t\t\"category\": \"fiction\",\n\t\t\t\t\"author\": \"Evelyn Waugh\",\n\t\t\t\t\"title\": \"Sword of Honour\",\n\t\t\t\t\"price\": 12.99\n\t\t\t},\n\t\t\t{ \n\t\t\t\t\"category\": \"fiction\",\n\t\t\t\t\"author\": \"Herman Melville\",\n\t\t\t\t\"title\": \"Moby Dick\",\n\t\t\t\t\"isbn\": \"0-553-21311-3\",\n\t\t\t\t\"price\": 8.99\n\t\t\t},\n\t\t\t{ \n\t\t\t\t\"category\": \"fiction\",\n\t\t\t\t\"author\": \"J. R. R. Tolkien\",\n\t\t\t\t\"title\": \"The Lord of the Rings\",\n\t\t\t\t\"isbn\": \"0-395-19395-8\",\n\t\t\t\t\"price\": 22.99\n\t\t\t}\n\t\t],\n\t\t\"bicycle\": {\n\t\t\t\"color\": \"red\",\n\t\t\t\"price\": 19.95\n\t\t}\n\t}\n}<\/code><\/pre>\n\n\n\nExample 1<\/h4>\n\n\n\n
Using JSONPath to find the names of all authors.<\/p>\n\n\n\n
var response = jsonPath(store , \"$..author\").toJSONString();<\/code><\/pre>\n\n\n\nProgram output:<\/p>\n\n\n\n
[\n\"Nigel Rees\",\n\"Evelyn Waugh\",\n\"Herman Melville\",\n\"J. R. R. Tolkien\"\n]<\/code><\/pre>\n\n\n\nExample 2<\/h4>\n\n\n\n
Using JSONPath to find the details for book number 4. The array index is zero-based.<\/p>\n\n\n\n
var response = jsonPath(store , \"$..book[3]\").toJSONString();<\/code><\/pre>\n\n\n\nProgram output:<\/p>\n\n\n\n
[\n\t{\n\t\t\"category\":\"fiction\",\n\t\t\"author\":\"J. R. R. Tolkien\",\n\t\t\"title\":\"The Lord of the Rings\",\n\t\t\"isbn\":\"0-395-19395-8\",\n\t\t\"price\":22.99\n\t}\n]<\/code><\/pre>\n\n\n\n<\/p>\n","protected":false},"excerpt":{"rendered":"
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 JSONPath JavaScript File Node npm install JSONPath PHP … Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":249,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[16],"tags":[17,22],"_links":{"self":[{"href":"https:\/\/restfulapi.net\/wp-json\/wp\/v2\/posts\/273"}],"collection":[{"href":"https:\/\/restfulapi.net\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/restfulapi.net\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/restfulapi.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/restfulapi.net\/wp-json\/wp\/v2\/comments?post=273"}],"version-history":[{"count":0,"href":"https:\/\/restfulapi.net\/wp-json\/wp\/v2\/posts\/273\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/restfulapi.net\/wp-json\/wp\/v2\/media\/249"}],"wp:attachment":[{"href":"https:\/\/restfulapi.net\/wp-json\/wp\/v2\/media?parent=273"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/restfulapi.net\/wp-json\/wp\/v2\/categories?post=273"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/restfulapi.net\/wp-json\/wp\/v2\/tags?post=273"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}