{"id":12067,"date":"2015-03-22T23:44:00","date_gmt":"2015-03-22T22:44:00","guid":{"rendered":"http:\/\/touk.pl\/blog\/?guid=31b53e4947b4a5fcebd0a69e54a9d321"},"modified":"2022-07-27T15:58:48","modified_gmt":"2022-07-27T13:58:48","slug":"need-to-make-a-quick-json-fixes-jsonpath-for-rescue","status":"publish","type":"post","link":"https:\/\/touk.pl\/blog\/2015\/03\/22\/need-to-make-a-quick-json-fixes-jsonpath-for-rescue\/","title":{"rendered":"Need to make a quick json fixes &#8211; JSONPath for rescue"},"content":{"rendered":"<p>From time to time I have a need to do some fixes in my json data. In a world of flat files I do this with grep\/sed\/awk tool chain. How to handle it for JSON? Searching for a solution I came across the <strong>JSONPath<\/strong>. It quite mature tool (from 2007) but I haven&#8217;t hear about it so I decided to share my experience with others. First of all you can try it without pain online: <a href=\"http:\/\/jsonpath.curiousconcept.com\/\">http:\/\/jsonpath.curiousconcept.com\/<\/a>. Full syntax is described at <a href=\"http:\/\/goessner.net\/articles\/JsonPath\/\">http:\/\/goessner.net\/articles\/JsonPath\/<\/a> <a name=\"more\"><\/a> But also you can download python binding and run it from command line:<\/p>\n<pre style=\"line-height: 100%;font-family: monospace;background-color: #ffffff;padding: 4px;font-size: 12pt;border: 0.01mm solid #000000\"><span style=\"background-color: #ffffff\">$ sudo apt-get install python-jsonpath-rw\n$ sudo apt-get install python-setuptools\n$ sudo easy_install -U jsonpath<\/span><\/pre>\n<p>After that you can use inside python or with simple cli wrapper:<\/p>\n<pre style=\"line-height: 100%;font-family: monospace;background-color: #ffffff;padding: 4px;font-size: 12pt;border: 0.01mm solid #000000\"><span style=\"color: #808080;background-color: #ffffff;font-style: italic\">#!\/usr\/bin\/python\n<\/span><span style=\"color: #000080;background-color: #ffffff;font-weight: bold\">import<\/span><span style=\"background-color: #ffffff\"> sys, json, jsonpath\n\npath = sys.argv[<\/span><span style=\"color: #0000ff;background-color: #ffffff\">1<\/span><span style=\"background-color: #ffffff\">]\n\nresult = jsonpath.jsonpath(json.load(sys.stdin), path)\n<\/span><span style=\"color: #000080;background-color: #ffffff;font-weight: bold\">print<\/span><span style=\"background-color: #ffffff\"> json.dumps(result, indent=<\/span><span style=\"color: #0000ff;background-color: #ffffff\">2<\/span><span style=\"background-color: #ffffff\">)<\/span><\/pre>\n<p>\u2026 you can use it in your shell e.g. for json:<\/p>\n<pre style=\"line-height: 100%;font-family: monospace;background-color: #ffffff;padding: 4px;font-size: 12pt;border: 0.01mm solid #000000\"><span style=\"background-color: #ffffff\">{\n  <\/span><span style=\"color: #008000;background-color: #ffffff;font-weight: bold\">\"store\"<\/span><span style=\"background-color: #ffffff\">: {\n    <\/span><span style=\"color: #008000;background-color: #ffffff;font-weight: bold\">\"book\"<\/span><span style=\"background-color: #ffffff\">: [\n      {\n        <\/span><span style=\"color: #008000;background-color: #ffffff;font-weight: bold\">\"category\"<\/span><span style=\"background-color: #ffffff\">: <\/span><span style=\"color: #008000;background-color: #ffffff;font-weight: bold\">\"reference\"<\/span><span style=\"background-color: #ffffff\">,\n        <\/span><span style=\"color: #008000;background-color: #ffffff;font-weight: bold\">\"author\"<\/span><span style=\"background-color: #ffffff\">: <\/span><span style=\"color: #008000;background-color: #ffffff;font-weight: bold\">\"Nigel Rees\"<\/span><span style=\"background-color: #ffffff\">,\n        <\/span><span style=\"color: #008000;background-color: #ffffff;font-weight: bold\">\"title\"<\/span><span style=\"background-color: #ffffff\">: <\/span><span style=\"color: #008000;background-color: #ffffff;font-weight: bold\">\"Sayings of the Century\"<\/span><span style=\"background-color: #ffffff\">,\n        <\/span><span style=\"color: #008000;background-color: #ffffff;font-weight: bold\">\"price\"<\/span><span style=\"background-color: #ffffff\">: <\/span><span style=\"color: #0000ff;background-color: #ffffff\">8.95\n<\/span><span style=\"background-color: #ffffff\">      },\n      {\n        <\/span><span style=\"color: #008000;background-color: #ffffff;font-weight: bold\">\"category\"<\/span><span style=\"background-color: #ffffff\">: <\/span><span style=\"color: #008000;background-color: #ffffff;font-weight: bold\">\"fiction\"<\/span><span style=\"background-color: #ffffff\">,\n        <\/span><span style=\"color: #008000;background-color: #ffffff;font-weight: bold\">\"author\"<\/span><span style=\"background-color: #ffffff\">: <\/span><span style=\"color: #008000;background-color: #ffffff;font-weight: bold\">\"Evelyn Waugh\"<\/span><span style=\"background-color: #ffffff\">,\n        <\/span><span style=\"color: #008000;background-color: #ffffff;font-weight: bold\">\"title\"<\/span><span style=\"background-color: #ffffff\">: <\/span><span style=\"color: #008000;background-color: #ffffff;font-weight: bold\">\"Sword of Honour\"<\/span><span style=\"background-color: #ffffff\">,\n        <\/span><span style=\"color: #008000;background-color: #ffffff;font-weight: bold\">\"price\"<\/span><span style=\"background-color: #ffffff\">: <\/span><span style=\"color: #0000ff;background-color: #ffffff\">12.99\n<\/span><span style=\"background-color: #ffffff\">      },\n      {\n        <\/span><span style=\"color: #008000;background-color: #ffffff;font-weight: bold\">\"category\"<\/span><span style=\"background-color: #ffffff\">: <\/span><span style=\"color: #008000;background-color: #ffffff;font-weight: bold\">\"fiction\"<\/span><span style=\"background-color: #ffffff\">,\n        <\/span><span style=\"color: #008000;background-color: #ffffff;font-weight: bold\">\"author\"<\/span><span style=\"background-color: #ffffff\">: <\/span><span style=\"color: #008000;background-color: #ffffff;font-weight: bold\">\"Herman Melville\"<\/span><span style=\"background-color: #ffffff\">,\n        <\/span><span style=\"color: #008000;background-color: #ffffff;font-weight: bold\">\"title\"<\/span><span style=\"background-color: #ffffff\">: <\/span><span style=\"color: #008000;background-color: #ffffff;font-weight: bold\">\"Moby Dick\"<\/span><span style=\"background-color: #ffffff\">,\n        <\/span><span style=\"color: #008000;background-color: #ffffff;font-weight: bold\">\"isbn\"<\/span><span style=\"background-color: #ffffff\">: <\/span><span style=\"color: #008000;background-color: #ffffff;font-weight: bold\">\"0-553-21311-3\"<\/span><span style=\"background-color: #ffffff\">,\n        <\/span><span style=\"color: #008000;background-color: #ffffff;font-weight: bold\">\"price\"<\/span><span style=\"background-color: #ffffff\">: <\/span><span style=\"color: #0000ff;background-color: #ffffff\">8.99\n<\/span><span style=\"background-color: #ffffff\">      },\n      {\n        <\/span><span style=\"color: #008000;background-color: #ffffff;font-weight: bold\">\"category\"<\/span><span style=\"background-color: #ffffff\">: <\/span><span style=\"color: #008000;background-color: #ffffff;font-weight: bold\">\"fiction\"<\/span><span style=\"background-color: #ffffff\">,\n        <\/span><span style=\"color: #008000;background-color: #ffffff;font-weight: bold\">\"author\"<\/span><span style=\"background-color: #ffffff\">: <\/span><span style=\"color: #008000;background-color: #ffffff;font-weight: bold\">\"J. R. R. Tolkien\"<\/span><span style=\"background-color: #ffffff\">,\n        <\/span><span style=\"color: #008000;background-color: #ffffff;font-weight: bold\">\"title\"<\/span><span style=\"background-color: #ffffff\">: <\/span><span style=\"color: #008000;background-color: #ffffff;font-weight: bold\">\"The Lord of the Rings\"<\/span><span style=\"background-color: #ffffff\">,\n        <\/span><span style=\"color: #008000;background-color: #ffffff;font-weight: bold\">\"isbn\"<\/span><span style=\"background-color: #ffffff\">: <\/span><span style=\"color: #008000;background-color: #ffffff;font-weight: bold\">\"0-395-19395-8\"<\/span><span style=\"background-color: #ffffff\">,\n        <\/span><span style=\"color: #008000;background-color: #ffffff;font-weight: bold\">\"price\"<\/span><span style=\"background-color: #ffffff\">: <\/span><span style=\"color: #0000ff;background-color: #ffffff\">22.99\n<\/span><span style=\"background-color: #ffffff\">      }\n    ],\n    <\/span><span style=\"color: #008000;background-color: #ffffff;font-weight: bold\">\"bicycle\"<\/span><span style=\"background-color: #ffffff\">: {\n      <\/span><span style=\"color: #008000;background-color: #ffffff;font-weight: bold\">\"color\"<\/span><span style=\"background-color: #ffffff\">: <\/span><span style=\"color: #008000;background-color: #ffffff;font-weight: bold\">\"red\"<\/span><span style=\"background-color: #ffffff\">,\n      <\/span><span style=\"color: #008000;background-color: #ffffff;font-weight: bold\">\"price\"<\/span><span style=\"background-color: #ffffff\">: <\/span><span style=\"color: #0000ff;background-color: #ffffff\">19.95\n<\/span><span style=\"background-color: #ffffff\">    }\n  }\n}<\/span><\/pre>\n<p>You can print only book nodes with price lower than 10 by:<\/p>\n<pre style=\"line-height: 100%;font-family: monospace;background-color: #ffffff;padding: 4px;font-size: 12pt;border: 0.01mm solid #000000\"><span style=\"background-color: #ffffff\">$ jsonpath '$..book[?(@.price &lt; 10)]' &lt; books.json<\/span><\/pre>\n<p>Result:<\/p>\n<pre style=\"line-height: 100%;font-family: monospace;background-color: #ffffff;padding: 4px;font-size: 12pt;border: 0.01mm solid #000000\"><span style=\"background-color: #ffffff\">[\n  {\n    <\/span><span style=\"color: #008000;background-color: #ffffff;font-weight: bold\">\"category\"<\/span><span style=\"background-color: #ffffff\">: <\/span><span style=\"color: #008000;background-color: #ffffff;font-weight: bold\">\"reference\"<\/span><span style=\"background-color: #ffffff\">,\n    <\/span><span style=\"color: #008000;background-color: #ffffff;font-weight: bold\">\"price\"<\/span><span style=\"background-color: #ffffff\">: <\/span><span style=\"color: #0000ff;background-color: #ffffff\">8.95<\/span><span style=\"background-color: #ffffff\">,\n    <\/span><span style=\"color: #008000;background-color: #ffffff;font-weight: bold\">\"title\"<\/span><span style=\"background-color: #ffffff\">: <\/span><span style=\"color: #008000;background-color: #ffffff;font-weight: bold\">\"Sayings of the Century\"<\/span><span style=\"background-color: #ffffff\">,\n    <\/span><span style=\"color: #008000;background-color: #ffffff;font-weight: bold\">\"author\"<\/span><span style=\"background-color: #ffffff\">: <\/span><span style=\"color: #008000;background-color: #ffffff;font-weight: bold\">\"Nigel Rees\"\n<\/span><span style=\"background-color: #ffffff\">  },\n  {\n    <\/span><span style=\"color: #008000;background-color: #ffffff;font-weight: bold\">\"category\"<\/span><span style=\"background-color: #ffffff\">: <\/span><span style=\"color: #008000;background-color: #ffffff;font-weight: bold\">\"fiction\"<\/span><span style=\"background-color: #ffffff\">,\n    <\/span><span style=\"color: #008000;background-color: #ffffff;font-weight: bold\">\"price\"<\/span><span style=\"background-color: #ffffff\">: <\/span><span style=\"color: #0000ff;background-color: #ffffff\">8.99<\/span><span style=\"background-color: #ffffff\">,\n    <\/span><span style=\"color: #008000;background-color: #ffffff;font-weight: bold\">\"title\"<\/span><span style=\"background-color: #ffffff\">: <\/span><span style=\"color: #008000;background-color: #ffffff;font-weight: bold\">\"Moby Dick\"<\/span><span style=\"background-color: #ffffff\">,\n    <\/span><span style=\"color: #008000;background-color: #ffffff;font-weight: bold\">\"isbn\"<\/span><span style=\"background-color: #ffffff\">: <\/span><span style=\"color: #008000;background-color: #ffffff;font-weight: bold\">\"0-553-21311-3\"<\/span><span style=\"background-color: #ffffff\">,\n    <\/span><span style=\"color: #008000;background-color: #ffffff;font-weight: bold\">\"author\"<\/span><span style=\"background-color: #ffffff\">: <\/span><span style=\"color: #008000;background-color: #ffffff;font-weight: bold\">\"Herman Melville\"\n<\/span><span style=\"background-color: #ffffff\">  }\n]<\/span><\/pre>\n<p>Have a nice JSON hacking!<\/p>\n","protected":false},"excerpt":{"rendered":"From time to time I have a need to do some fixes in my json data. In a world of flat files I do this with grep\/sed\/awk tool chain. How to handle it for JSON? Searching for a solution I came across the JSONPath. It quite mature tool (from 2007) but I haven&#8217;t hear about it so I decided to share my experience with others.\nFirst of all you can try it without pain online: http:\/\/jsonpath.curiousconcept.com\/. Full syntax is described at http:\/\/goessner.net\/articles\/JsonPath\/\n\nBut also you can download python binding and run it from command line:\n$ sudo apt-get install python-jsonpath-rw$ sudo apt-get install python-setuptools$ sudo easy_install -U jsonpath\nAfter that you can use inside python or with simple cli wrapper:\n#!\/usr\/bin\/pythonimport sys, json, jsonpathpath = sys.argv[1]result = jsonpath.jsonpath(json.load(sys.stdin), path)print json.dumps(result, indent=2)\n&hellip; you can use it in your shell e.g. for json:\n{\"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    }  }}\nYou can print only book nodes with price lower than 10 by:\n$ jsonpath '$..book[?(@.price \nResult:\n[  {\"category\": \"reference\",\"price\": 8.95,\"title\": \"Sayings of the Century\",\"author\": \"Nigel Rees\"  },  {\"category\": \"fiction\",\"price\": 8.99,\"title\": \"Moby Dick\",\"isbn\": \"0-553-21311-3\",\"author\": \"Herman Melville\"  }]\nHave a nice JSON hacking!From time to time I have a need to do some fixes in my json data. In a world of flat files I do this with grep\/sed\/awk tool chain. How to handle it for JSON? Searching for a solution I came across the JSONPath. It quite mature tool (from 2007) but I haven&#8217;t hear about it so I decided to share my experience with others.\n","protected":false},"author":28,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[283,537,107],"class_list":{"0":"post-12067","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-development-design","7":"tag-json","8":"tag-shell","9":"tag-xml"},"_links":{"self":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts\/12067","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/users\/28"}],"replies":[{"embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/comments?post=12067"}],"version-history":[{"count":9,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts\/12067\/revisions"}],"predecessor-version":[{"id":14624,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts\/12067\/revisions\/14624"}],"wp:attachment":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/media?parent=12067"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/categories?post=12067"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/tags?post=12067"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}