{"id":67,"date":"2024-10-14T14:23:50","date_gmt":"2024-10-14T14:23:50","guid":{"rendered":"https:\/\/itsmycode.com\/?p=67"},"modified":"2024-10-14T14:23:50","modified_gmt":"2024-10-14T14:23:50","slug":"python-jsonpath","status":"publish","type":"post","link":"https:\/\/itsmycode.com\/python-jsonpath\/","title":{"rendered":"Python JSONPath"},"content":{"rendered":"\n<p><strong>JSONPath <\/strong>is an expression language that is used to parse the JSON data in Python. JSONPath is similar to XPath in XML, where we parse the XML data.&nbsp;<\/p>\n\n\n\n<p>JSONPath provides a simpler syntax to query JSON data and get the desired value in Python. Using JSONPath will be the more efficient way to parse and query JSON data as we don\u2019t have to load the entire JSON data. This approach is more memory-optimized compared to any other way of querying JSON.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-jsonpath-library-in-python\">JSONPath Library in Python<\/h2>\n\n\n\n<p>There are many JSONPath libraries for Python, and the most popular one is the&nbsp;<strong>jsonpath-ng&nbsp;<\/strong>library. It\u2019s written in the native Python language and supports both Python 2 and Python 3 versions.<\/p>\n\n\n\n<p><strong>jsonpath-ng&nbsp;<\/strong>is the final implementation of JSONPath for Python that aims for standard-compliant including arithmetic and binary comparison operators s, as defined in the original&nbsp;<a href=\"http:\/\/goessner.net\/articles\/JsonPath\/\" target=\"_blank\" rel=\"noreferrer noopener\">JSONPath proposal<\/a>.&nbsp;<\/p>\n\n\n\n<p>This packages merges both&nbsp;<a target=\"_blank\" href=\"https:\/\/github.com\/kennknowles\/python-jsonpath-rw\" rel=\"noreferrer noopener\">jsonpath-rw<\/a>&nbsp;and&nbsp;<a target=\"_blank\" href=\"https:\/\/pypi.python.org\/pypi\/jsonpath-rw-ext\/\" rel=\"noreferrer noopener\">jsonpath-rw-ext<\/a>&nbsp;and provides several AST API enhancements, such as the ability to update or remove nodes in the tree.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-installing-jsonpath-ng-module\">Installing jsonpath-ng Module<\/h2>\n\n\n\n<p>To install&nbsp;<strong>jsonpath-ng&nbsp;<\/strong>library, use the below pip install command.&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">pip install --upgrade jsonpath-ng<\/pre>\n\n\n\n<p>The above command will install the latest version of the&nbsp;<strong>jsonpath-ng<\/strong>&nbsp;library on your machine. Once installed, you can import in the Python IDE using the below code.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import jsonpath_ng<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-jsonpath-operators\">Jsonpath operators:<\/h2>\n\n\n\n<p>Below are the list of operators you can use for getting json data values.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Syntax<\/th><th>Meaning<\/th><\/tr><\/thead><tbody><tr><td><em>jsonpath1<\/em>&nbsp;.&nbsp;<em>jsonpath2<\/em><\/td><td>All nodes matched by&nbsp;<em>jsonpath2<\/em>&nbsp;starting at any node matching&nbsp;<em>jsonpath1<\/em><\/td><\/tr><tr><td><em>jsonpath<\/em>&nbsp;[&nbsp;<em>whatever<\/em>&nbsp;]<\/td><td>Same as&nbsp;<em>jsonpath<\/em>.<em>whatever<\/em><\/td><\/tr><tr><td><em>jsonpath1<\/em>&nbsp;..&nbsp;<em>jsonpath2<\/em><\/td><td>All nodes matched by&nbsp;<em>jsonpath2<\/em>&nbsp;that descend from any node matching&nbsp;<em>jsonpath1<\/em><\/td><\/tr><tr><td><em>jsonpath1<\/em>&nbsp;where&nbsp;<em>jsonpath2<\/em><\/td><td>Any nodes matching&nbsp;<em>jsonpath1<\/em>&nbsp;with a child matching&nbsp;<em>jsonpath2<\/em><\/td><\/tr><tr><td><em>jsonpath1<\/em>&nbsp;|&nbsp;<em>jsonpath2<\/em><\/td><td>Any nodes matching the union of&nbsp;<em>jsonpath1<\/em>&nbsp;and&nbsp;<em>jsonpath2<\/em><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-parsing-a-simple-json-data-using-jsonpath\">Parsing a Simple JSON Data using JSONPath<\/h2>\n\n\n\n<p>A Simple example of parsing the JSON and fetching the JSON value using the attribute key.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Program to parse JSON data in Python \nimport json\nfrom jsonpath_ng import jsonpath, parse\n\nemployee_data = '{\"id\":1, \"first_name\":\"Chandler\" , \"last_name\":\"Bing\"}'\njson_data = json.loads(employee_data)\n\njsonpath_expr= parse('$.first_name')\nfirst_name = jsonpath_expr.find(json_data)\n\nprint(\"The First Name of the employee is: \", first_name&#91;0].value)<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>The First Name of the employee is  Chandler<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-parsing-a-json-array-using-jsonpath-expression\">Parsing a Json Array using JSONPath Expression<\/h2>\n\n\n\n<p>The JSON key contains the list of values and uses the JSON Path expression. We can parse and query the exact field values of the JSON.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{\n    \"books\": &#91;\n        {\n            \"category\": \"reference\",\n            \"author\": \"Nigel Rees\",\n            \"title\": \"Sayings of the Century\",\n            \"isbn\": \"6-246-2356-8\",\n            \"price\": 8.95\n        },\n        {\n            \"category\": \"fiction\",\n            \"author\": \"Evelyn Waugh\",\n            \"title\": \"Sword of Honour\",\n            \"isbn\": \"5-444-34234-8\",\n            \"price\": 12.99\n        },\n        {\n            \"category\": \"fiction\",\n            \"author\": \"Herman Melville\",\n            \"title\": \"Moby Dick\",\n            \"isbn\": \"0-553-21311-3\",\n            \"price\": 8.99\n        },\n        {\n            \"category\": \"fiction\",\n            \"author\": \"J. R. R. Tolkien\",\n            \"title\": \"The Lord of the Rings\",\n            \"isbn\": \"0-395-19395-8\",\n            \"price\": 22.99\n        }\n    ]\n}<\/code><\/pre>\n\n\n\n<p>In the above JSON data, if we need the list of all ISBN of the book, we can use the below code to get the data using JSONPath expression as shown below.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Program to parse JSON data in Python \nimport json\nfrom jsonpath_ng import jsonpath, parse\n\nwith open(\"books.json\", 'r') as json_file:\n    json_data = json.load(json_file)\n\njsonpath_expression = parse('books&#91;*].isbn')\n\nfor match in jsonpath_expression.find(json_data):\n    print(f'Books ISBN: {match.value}')\n\n\n<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Books ISBN: 6-246-2356-8\nBooks ISBN: 5-444-34234-8\nBooks ISBN: 0-553-21311-3\nBooks ISBN: 0-395-19395-8<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>JSONPath is an expression language that is used to parse the JSON data in Python. JSONPath is similar to XPath in XML, where we parse the XML data.&nbsp; JSONPath provides a simpler syntax to query JSON data and get the desired value in Python. Using JSONPath will be the more efficient way to parse and [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":68,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-67","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/itsmycode.com\/wp-json\/wp\/v2\/posts\/67","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itsmycode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itsmycode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itsmycode.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/itsmycode.com\/wp-json\/wp\/v2\/comments?post=67"}],"version-history":[{"count":1,"href":"https:\/\/itsmycode.com\/wp-json\/wp\/v2\/posts\/67\/revisions"}],"predecessor-version":[{"id":69,"href":"https:\/\/itsmycode.com\/wp-json\/wp\/v2\/posts\/67\/revisions\/69"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/itsmycode.com\/wp-json\/wp\/v2\/media\/68"}],"wp:attachment":[{"href":"https:\/\/itsmycode.com\/wp-json\/wp\/v2\/media?parent=67"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itsmycode.com\/wp-json\/wp\/v2\/categories?post=67"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itsmycode.com\/wp-json\/wp\/v2\/tags?post=67"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}