Giter Site home page Giter Site logo

jsonpath-perl's Introduction

JSONPath (Perl) - XPath for JSON and Perl

JSONPath is the brainchild of Stephan Goessner. It takes the basic concept of XPATH and, using similar syntax, applies it to JSON data structures. This documentation is taken directly from Stephan's JSONPath page with minor tweaks specific to the Perl port. You can find the JavaScript, PHP, and C# implementation of it on the JSONPath page at Google Code.

JSONPath is distributed under the MIT License.

JSONPath expressions

JSONPath expressions always refer to a JSON structure in the same way as XPath expression are used in combination with an XML document. Since a JSON structure is usually anonymous and doesn't necessarily have a "root member object" JSONPath assumes the abstract name $ assigned to the outer level object.

JSONPath expressions can use the dot notation

$.store.book[0].title

or the bracket notation

$['store']['book'][0]['title']

for input pathes. Internal or output pathes will always be converted to the more general bracket notation.

JSONPath allows the wildcard symbol * for member names and array indices. It borrows the descendant operator '..' from E4X and the array slice syntax proposal [start:end:step] from ECMASCRIPT 4.

Expressions of the underlying scripting language () can be used as an alternative to explicit names or indices as in

$.store.book[(@.length-1)].title

using the symbol '@' for the current object. Filter expressions are supported via the syntax ?() as in

$.store.book[?(@.price < 10)].title

XPath has a lot more to offer (Location paths in not abbreviated syntax, operators and functions) than listed here. Moreover there is a remarkable difference how the subscript operator works in Xpath and JSONPath.

* Square brackets in XPath expressions always operate on the node set resulting from the previous path fragment. Indices always start by 1.
* With JSONPath square brackets operate on the object or array addressed by the previous path fragment. Indices always start by 0. 

JSONPath examples

Let's practice JSONPath expressions by some more examples. We start with a simple JSON structure built after an XML example representing a bookstore.

{ "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
	}
  }
}

Here are examples of all the types of JSONPath expressions you could perform on this and what you should expect to see as a result. We've also included the comparable XPath expressions.

XPath               JSONPath                Result
/store/book/author   $.store.book[*].author  the authors of all books in the store
//author             $..author               all authors
/store/*             $.store.*               all things in store, which are some books and a red bicycle.
/store//price        $.store..price          the price of everything in the store.
//book[3]            $..book[2]              the third book
//book[last()]       $..book[(@.length-1)]
//book[position()<3] $..book[-1:]            the last book in order.
                     $..book[0,1]
                     $..book[:2]             the first two books
//book[isbn]         $..book[?(@.isbn)]      filter all books with isbn number
//book[price<10]     $..book[?(@.price<10)]  filter all books cheapier than 10
//*                  $..*                    all Elements in XML document. All members of JSON structure.
???                  $.store.!               all the keys in the store hash (bicycle, book)
                     $..book[?(@.ratings><'good')]
                                            all the books with a ratings element that contains the word "good"
                                            This could be a key in a hash, an item in an array,
                                            or a substring of a string.

JSONPath implementation

JSONPath.pm is a simple perl class, ported from the Javascript and PHP versions, that can be wrapped within a script like jsonpath.pl to give command line inspection into a JSON file or leveraged in a larger application. To use it simply parse the JSON with your favorite JSON library, create an instance of JSONPath, and pass the parsed json to it's run method along with your JSONPath expression

use JSON;
my $json_structure = from_json($raw_json);
my $jp = JSONPath->new();
my $raw_result = $jp->run($json_structure, "$..author"); # either a data structure or zero
print to_json($raw_result, {utf8 => 1, pretty => 1}) . "\n";

The run method also takes an optional third argument, a hash with any options. Currently the only supported option is "result_type" which can be "VALUE" or "PATH".

the example results in the following arrays:

res1:
[ "Nigel Rees",
  "Evelyn Waugh",
  "Herman Melville",
  "J. R. R. Tolkien"

]

res2:
[ "$['store']['book'][0]['author']",
  "$['store']['book'][1]['author']",
  "$['store']['book'][2]['author']",
  "$['store']['book'][3]['author']"
]

Please note, that the return value of jsonPath is an array, which is also a valid JSON structure. So you might want to apply jsonPath to the resulting structure again or use one of your favorite array methods as sort with it.

Issues

  • Currently only single quotes allowed inside of JSONPath expressions.
  • Script expressions inside of JSONPath locations are currently not recursively evaluated by jsonPath. Only the global $ and local @ symbols are expanded by a simple regular expression.
  • An alternative for jsonPath to return false in case of no match may be to return an empty array in future.

jsonpath-perl's People

Contributors

masukomi avatar snark avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

jsonpath-perl's Issues

regex failed

i use jsonpath.jsonpath(raw_parse_json, "$.labels.[?(@.real_class=~ /.*_V/i)]",debug=1)
it returns False
but it works on http://jsonpath.herokuapp.com/?path=$.store.book[]%28https://note.youdao.com/%29%E6%9D%A5%E9%AA%8C%E8%AF%81%E4%BD%A0%E7%9A%84%E8%A1%A8%E8%BE%BE%E5%BC%8F%E5%93%A6~

json
{'file_id': '11433639_1', 'classes': ['发货人', '发货人地址', '收货人', '收货人地址', '发票号', '发票日期', '合同编号', '支付方式', '箱号', '唛头', '品名', '件数', '毛重', '净重', '体积', '总件数', '总毛重', '总净重', '总体积', '起运港', '目的港', '卸货港', 'TOTAL', '发货人_V', '发货人地址_V', '收货人_V', '收货人地址_V', '发票号_V', '发票日期_V', '合同编号_V', '支付方式_V', '箱号_V', '唛头_V', '品名_V', '件数_V', '毛重_V', '净重_V', '体积_V', '总件数_V', '总毛重_V', '总净重_V', '总体积_V', '汇总备注_V', '起运港_V', '目的港_V', '卸货港_V'], 'labels': [{'index': 1637289349093, 'text': 'Application\r\n发货人:', 'real_class': '发货人', 'state': True, 'position': {'left': 84.32708333333333, 'top': 538.2052083333333, 'bottom': 590.2895833333333, 'right': 533.2447916666666}}, {'index': 1637289349277, 'text': '长春美迪康科技发展有限公司\nMDK Industry Co.,Ltd', 'real_class': '发货人_V', 'state': True, 'position': {'left': 76.88645833333332, 'top': 595.25, 'bottom': 726.7010416666666, 'right': 853.1916666666666}}, {'index': 1637289349409, 'text': 'Add:', 'real_class': '发货人地址', 'state': True, 'position': {'left': 79.36666666666666, 'top': 729.18125, 'bottom': 791.1864583333332, 'right': 178.575}}, {'index': 1637289349560, 'text': 'Add:Room456,The first building,Anlian International,South o\nf Heyuan Road,East of Huaguang Street,High Tech Area\nTel:+86-0431-81976357', 'real_class': '发货人地址_V', 'state': True, 'position': {'left': 71.92604166666666, 'top': 726.7010416666666, 'bottom': 922.6374999999999, 'right': 1264.90625}}, {'index': 1637289349709, 'text': 'Receiver', 'real_class': '收货人', 'state': True, 'position': {'left': 74.40625, 'top': 979.6822916666666, 'bottom': 1039.2072916666666, 'right': 307.5458333333333}}, {'index': 1637289349843, 'text': '收货人:', 'real_class': '收货人', 'state': True, 'position': {'left': 81.846875, 'top': 1044.1677083333332, 'bottom': 1128.4947916666665, 'right': 245.54062499999998}}, {'index': 1637289349994, 'text': 'Jamrock\nTraders Ltd', 'real_class': '收货人_V', 'state': True, 'position': {'left': 84.32708333333333, 'top': 1044.1677083333332, 'bottom': 1121.0541666666666, 'right': 840.790625}}, {'index': 1637289350277, 'text': 'Address:', 'real_class': '收货人地址', 'state': True, 'position': {'left': 84.32708333333333, 'top': 1118.5739583333332, 'bottom': 1168.178125, 'right': 238.1}}, {'index': 1637289350444, 'text': 'Shop 7 Taj Mahal Plaza Ocho Rios Jamaica\nReceivor:Bobby Thakurani\nTel:8768225000', 'real_class': '收货人地址_V', 'state': True, 'position': {'left': 71.92604166666666, 'top': 1103.6927083333333, 'bottom': 1299.6291666666666, 'right': 1011.925}}, {'index': 1637289350596, 'text': 'INVOICENO.发票号:', 'real_class': '发票号', 'state': True, 'position': {'left': 1257.4656249999998, 'top': 510.92291666666665, 'bottom': 622.5322916666667, 'right': 1832.8739583333331}}, {'index': 1637289350744, 'text': 'MDK20210610S', 'real_class': '发票号_V', 'state': True, 'position': {'left': 1326.9114583333333, 'top': 632.453125, 'bottom': 721.7406249999999, 'right': 1755.9875}}, {'index': 1637289350885, 'text': 'DATE\r\n发票日期:', 'real_class': '发票日期', 'state': True, 'position': {'left': 1842.7947916666665, 'top': 520.84375, 'bottom': 632.453125, 'right': 2266.9104166666666}}, {'index': 1637289351045, 'text': '2021-06-10', 'real_class': '发票日期_V', 'state': True, 'position': {'left': 1870.0770833333331, 'top': 639.89375, 'bottom': 729.18125, 'right': 2199.9447916666663}}, {'index': 1637289603581, 'text': 'FROM\r\n起运地:', 'real_class': '起运港', 'state': True, 'position': {'left': 1272.346875, 'top': 741.5822916666666, 'bottom': 813.5083333333333, 'right': 1656.7791666666665}}, {'index': 1637289603733, 'text': 'Dalian China', 'real_class': '起运港_V', 'state': True, 'position': {'left': 1302.109375, 'top': 833.3499999999999, 'bottom': 912.7166666666666, 'right': 1666.6999999999998}}, {'index': 1637289603881, 'text': 'TO\r\n目的地:', 'real_class': '目的港', 'state': True, 'position': {'left': 1847.7552083333333, 'top': 736.6218749999999, 'bottom': 823.4291666666667, 'right': 2190.023958333333}}, {'index': 1637289604031, 'text': 'USA', 'real_class': '目的港_V', 'state': True, 'position': {'left': 1840.3145833333333, 'top': 835.8302083333333, 'bottom': 920.1572916666667, 'right': 2038.7312499999998}}, {'index': 1637289604165, 'text': 'PAYMENT TERMS\n付款方式:', 'real_class': '支付方式', 'state': True, 'position': {'left': 1269.8666666666666, 'top': 927.5979166666666, 'bottom': 987.1229166666666, 'right': 2013.9291666666666}}, {'index': 1637289604314, 'text': 'T/T\r\n电汇', 'real_class': '支付方式_V', 'state': True, 'position': {'left': 1279.7875, 'top': 982.1624999999999, 'bottom': 1066.4895833333333, 'right': 1542.6895833333333}}, {'index': 1637289604448, 'text': '装运港:\nPort of Loading', 'real_class': '起运港', 'state': True, 'position': {'left': 1267.3864583333332, 'top': 1068.9697916666667, 'bottom': 1116.09375, 'right': 1974.2458333333332}}, {'index': 1637289604597, 'text': 'China', 'real_class': '起运港_V', 'state': True, 'position': {'left': 1312.0302083333333, 'top': 1140.8958333333333, 'bottom': 1207.8614583333333, 'right': 1517.8874999999998}}, {'index': 1637289604732, 'text': 'Productname\r\n产品品名', 'real_class': '品名', 'state': True, 'position': {'left': 89.2875, 'top': 1597.2541666666666, 'bottom': 1815.5124999999998, 'right': 543.165625}}, {'index': 1637289736789, 'text': 'NetWeight\r\n净重', 'real_class': '净重', 'state': True, 'position': {'left': 1545.1697916666665, 'top': 1607.175, 'bottom': 1817.9927083333332, 'right': 1815.5124999999998}}, {'index': 1637289737264, 'text': 'Gross\r\nWeight\r\n毛重', 'real_class': '毛重', 'state': True, 'position': {'left': 1835.3541666666665, 'top': 1594.7739583333332, 'bottom': 1837.834375, 'right': 2056.092708333333}}, {'index': 1637289737731, 'text': 'VOLUM\r\nE\r\n体积\r\n3(M)', 'real_class': '体积', 'state': True, 'position': {'left': 2085.8552083333334, 'top': 1597.2541666666666, 'bottom': 1852.7156249999998, 'right': 2326.4354166666667}}, {'index': 1637289738699, 'text': '335', 'real_class': '总净重_V', 'state': True, 'position': {'left': 1622.0562499999999, 'top': 2363.6385416666667, 'bottom': 2495.0895833333334, 'right': 1785.75}}, {'index': 1637289738837, 'text': '355', 'real_class': '总毛重_V', 'state': True, 'position': {'left': 1872.5572916666665, 'top': 2371.0791666666664, 'bottom': 2477.7281249999996, 'right': 2070.973958333333}}, {'index': 1637289874876, 'text': '1.5CBM', 'real_class': '总体积_V', 'state': True, 'position': {'left': 2095.7760416666665, 'top': 2376.039583333333, 'bottom': 2495.0895833333334, 'right': 2341.3166666666666}}, {'index': 1637289875051, 'text': '合计:', 'real_class': 'TOTAL', 'state': True, 'position': {'left': 255.46145833333333, 'top': 2381, 'bottom': 2477.7281249999996, 'right': 458.83854166666663}}, {'index': 1637290098788, 'text': 'QTY\r\n数量', 'real_class': '件数', 'state': True, 'position': {'left': 652.2947916666666, 'top': 1612.1354166666665, 'bottom': 1805.5916666666665, 'right': 848.2312499999999}}, {'index': 1637290119311, 'text': 'CartonSize\r\n包装尺寸', 'real_class': '体积', 'state': True, 'position': {'left': 907.7562499999999, 'top': 1622.0562499999999, 'bottom': 1845.2749999999999, 'right': 1250.0249999999999}}, {'index': 1637290119665, 'text': 'Carton\r\nNo\r\n箱数', 'real_class': '件数', 'state': True, 'position': {'left': 1307.0697916666666, 'top': 1609.6552083333333, 'bottom': 1840.3145833333333, 'right': 1507.9666666666665}}, {'index': 1637290187884, 'text': '4000\r\nbarrels', 'real_class': '总件数_V', 'state': True, 'position': {'left': 615.0916666666666, 'top': 2351.2374999999997, 'bottom': 2524.852083333333, 'right': 840.790625}}, {'index': 1637290198500, 'text': '28cartons', 'real_class': '总件数_V', 'state': True, 'position': {'left': 1279.7875, 'top': 2366.1187499999996, 'bottom': 2480.208333333333, 'right': 1493.0854166666666}}, {'index': 1637307680794, 'text': '1500\r\nbarrels', 'real_class': '件数_V', 'state': True, 'position': {'left': 664.6958333333333, 'top': 1882.4781249999999, 'bottom': 2061.053125, 'right': 838.3104166666666}}, {'index': 1637307680946, 'text': '2500\r\nbarrels', 'real_class': '件数_V', 'state': True, 'position': {'left': 647.3343749999999, 'top': 2145.380208333333, 'bottom': 2314.0343749999997, 'right': 840.790625}}, {'index': 1637307681096, 'text': '55*27*30cm', 'real_class': '体积_V', 'state': True, 'position': {'left': 925.1177083333333, 'top': 1907.2802083333331, 'bottom': 2033.7708333333333, 'right': 1237.6239583333333}}, {'index': 1637307681247, 'text': '49*49*26cm', 'real_class': '体积_V', 'state': True, 'position': {'left': 902.7958333333332, 'top': 2152.820833333333, 'bottom': 2301.633333333333, 'right': 1220.2624999999998}}, {'index': 1637307681386, 'text': '15cartons', 'real_class': '件数_V', 'state': True, 'position': {'left': 1307.0697916666666, 'top': 1919.6812499999999, 'bottom': 2051.1322916666663, 'right': 1517.8874999999998}}, {'index': 1637307681530, 'text': '13carton', 'real_class': '件数_V', 'state': True, 'position': {'left': 1314.5104166666665, 'top': 2155.3010416666666, 'bottom': 2279.311458333333, 'right': 1498.0458333333333}}, {'index': 1637307845249, 'text': '170', 'real_class': '净重_V', 'state': True, 'position': {'left': 1614.615625, 'top': 1914.7208333333333, 'bottom': 2046.1718749999998, 'right': 1743.5864583333332}}, {'index': 1637307845427, 'text': '165', 'real_class': '净重_V', 'state': True, 'position': {'left': 1614.615625, 'top': 2162.7416666666663, 'bottom': 2301.633333333333, 'right': 1753.5072916666666}}, {'index': 1637307845560, 'text': '180', 'real_class': '毛重_V', 'state': True, 'position': {'left': 1877.5177083333333, 'top': 1907.2802083333331, 'bottom': 2028.8104166666665, 'right': 2028.8104166666665}}, {'index': 1637307845694, 'text': '175', 'real_class': '毛重_V', 'state': True, 'position': {'left': 1872.5572916666665, 'top': 2157.78125, 'bottom': 2289.2322916666667, 'right': 2006.4885416666666}}, {'index': 1637307845817, 'text': '0.67', 'real_class': '体积_V', 'state': True, 'position': {'left': 2135.459375, 'top': 1894.8791666666666, 'bottom': 2046.1718749999998, 'right': 2279.311458333333}}, {'index': 1637307845977, 'text': '0.811', 'real_class': '体积_V', 'state': True, 'position': {'left': 2115.617708333333, 'top': 2147.8604166666664, 'bottom': 2274.3510416666663, 'right': 2279.311458333333}}], 'text_infos': [{'bottom': 133, 'left': 1812, 'right': 1924, 'top': 80, 'value': 'MDK', 'valueShow': '', 'valueID': 0}, {'bottom': 133, 'left': 1936, 'right': 2096, 'top': 80, 'value': 'Industry', 'valueShow': '', 'valueID': 1}, {'bottom': 133, 'left': 2108, 'right': 2188, 'top': 80, 'value': 'Co.,', 'valueShow': '', 'valueID': 2}, {'bottom': 133, 'left': 2200, 'right': 2279, 'top': 80, 'value': 'Ltd.', 'valueShow': '', 'valueID': 3}, {'bottom': 449, 'left': 797, 'right': 1196, 'top': 353, 'value': 'PACKING', 'valueShow': '', 'valueID': 4}, {'bottom': 449, 'left': 1221, 'right': 1411, 'top': 353, 'value': 'LIST', 'valueShow': '', 'valueID': 5}, {'bottom': 428, 'left': 1434, 'right': 1698, 'top': 331, 'value': '装箱单', 'valueShow': '', 'valueID': 6}, {'bottom': 605, 'left': 92, 'right': 333, 'top': 553, 'value': 'Application', 'valueShow': '', 'valueID': 7}, {'bottom': 605, 'left': 344, 'right': 506, 'top': 544, 'value': '发货人:', 'valueShow': '', 'valueID': 8}, {'bottom': 612, 'left': 1290, 'right': 1500, 'top': 559, 'value': 'INVOICE', 'valueShow': '', 'valueID': 9}, {'bottom': 612, 'left': 1512, 'right': 1757, 'top': 559, 'value': 'NO.发票号:', 'valueShow': '', 'valueID': 10}, {'bottom': 612, 'left': 1864, 'right': 1994, 'top': 559, 'value': 'DATE', 'valueShow': '', 'valueID': 11}, {'bottom': 604, 'left': 2006, 'right': 2247, 'top': 551, 'value': '发票日期:', 'valueShow': '', 'valueID': 12}, {'bottom': 659, 'left': 92, 'right': 719, 'top': 606, 'value': '长春美迪康科技发展有限公司', 'valueShow': '', 'valueID': 13}, {'bottom': 712, 'left': 1390, 'right': 1721, 'top': 660, 'value': 'MDK20210610S', 'valueShow': '', 'valueID': 14}, {'bottom': 712, 'left': 1888, 'right': 2112, 'top': 660, 'value': '2021-06-10', 'valueShow': '', 'valueID': 15}, {'bottom': 727, 'left': 92, 'right': 195, 'top': 678, 'value': 'MDK', 'valueShow': '', 'valueID': 16}, {'bottom': 727, 'left': 206, 'right': 353, 'top': 678, 'value': 'Industry', 'valueShow': '', 'valueID': 17}, {'bottom': 727, 'left': 364, 'right': 437, 'top': 678, 'value': 'Co.,', 'valueShow': '', 'valueID': 18}, {'bottom': 727, 'left': 448, 'right': 510, 'top': 678, 'value': 'Ltd', 'valueShow': '', 'valueID': 19}, {'bottom': 789, 'left': 92, 'right': 289, 'top': 740, 'value': 'Add:Room', 'valueShow': '', 'valueID': 20}, {'bottom': 789, 'left': 300, 'right': 377, 'top': 740, 'value': '456,', 'valueShow': '', 'valueID': 21}, {'bottom': 789, 'left': 389, 'right': 457, 'top': 740, 'value': 'The', 'valueShow': '', 'valueID': 22}, {'bottom': 789, 'left': 469, 'right': 540, 'top': 740, 'value': 'first', 'valueShow': '', 'valueID': 23}, {'bottom': 789, 'left': 552, 'right': 709, 'top': 740, 'value': 'building,', 'valueShow': '', 'valueID': 24}, {'bottom': 789, 'left': 721, 'right': 841, 'top': 740, 'value': 'Anlian', 'valueShow': '', 'valueID': 25}, {'bottom': 789, 'left': 853, 'right': 1078, 'top': 740, 'value': 'International', 'valueShow': '', 'valueID': 26}, {'bottom': 789, 'left': 1089, 'right': 1100, 'top': 740, 'value': ',', 'valueShow': '', 'valueID': 27}, {'bottom': 789, 'left': 1112, 'right': 1215, 'top': 740, 'value': 'South', 'valueShow': '', 'valueID': 28}, {'bottom': 789, 'left': 1227, 'right': 1249, 'top': 740, 'value': 'o', 'valueShow': '', 'valueID': 29}, {'bottom': 812, 'left': 1290, 'right': 1437, 'top': 759, 'value': 'FROM', 'valueShow': '', 'valueID': 30}, {'bottom': 803, 'left': 1449, 'right': 1641, 'top': 750, 'value': '起运地:', 'valueShow': '', 'valueID': 31}, {'bottom': 812, 'left': 1864, 'right': 1933, 'top': 759, 'value': 'TO', 'valueShow': '', 'valueID': 32}, {'bottom': 803, 'left': 1944, 'right': 2137, 'top': 750, 'value': '目的地:', 'valueShow': '', 'valueID': 33}, {'bottom': 852, 'left': 92, 'right': 107, 'top': 803, 'value': 'f', 'valueShow': '', 'valueID': 34}, {'bottom': 852, 'left': 118, 'right': 255, 'top': 803, 'value': 'Heyuan', 'valueShow': '', 'valueID': 35}, {'bottom': 852, 'left': 266, 'right': 370, 'top': 803, 'value': 'Road,', 'valueShow': '', 'valueID': 36}, {'bottom': 852, 'left': 381, 'right': 457, 'top': 803, 'value': 'East', 'valueShow': '', 'valueID': 37}, {'bottom': 852, 'left': 468, 'right': 505, 'top': 803, 'value': 'of', 'valueShow': '', 'valueID': 38}, {'bottom': 852, 'left': 515, 'right': 697, 'top': 803, 'value': 'Huaguang', 'valueShow': '', 'valueID': 39}, {'bottom': 852, 'left': 708, 'right': 822, 'top': 803, 'value': 'Street,', 'valueShow': '', 'valueID': 40}, {'bottom': 852, 'left': 832, 'right': 920, 'top': 803, 'value': 'High', 'valueShow': '', 'valueID': 41}, {'bottom': 852, 'left': 932, 'right': 1020, 'top': 803, 'value': 'Tech', 'valueShow': '', 'valueID': 42}, {'bottom': 852, 'left': 1031, 'right': 1116, 'top': 803, 'value': 'Area', 'valueShow': '', 'valueID': 43}, {'bottom': 914, 'left': 92, 'right': 220, 'top': 865, 'value': 'Tel:+', 'valueShow': '', 'valueID': 44}, {'bottom': 914, 'left': 231, 'right': 569, 'top': 865, 'value': '86-0431-81976357', 'valueShow': '', 'valueID': 45}, {'bottom': 902, 'left': 1314, 'right': 1442, 'top': 849, 'value': 'Dalian', 'valueShow': '', 'valueID': 46}, {'bottom': 902, 'left': 1454, 'right': 1569, 'top': 849, 'value': 'China', 'valueShow': '', 'valueID': 47}, {'bottom': 903, 'left': 1864, 'right': 1960, 'top': 850, 'value': 'USA', 'valueShow': '', 'valueID': 48}, {'bottom': 991, 'left': 1290, 'right': 1351, 'top': 938, 'value': 'PA', 'valueShow': '', 'valueID': 49}, {'bottom': 991, 'left': 1347, 'right': 1706, 'top': 938, 'value': 'YMENTTERMS', 'valueShow': '', 'valueID': 50}, {'bottom': 982, 'left': 1718, 'right': 1959, 'top': 929, 'value': '付款方式:', 'valueShow': '', 'valueID': 51}, {'bottom': 1056, 'left': 92, 'right': 271, 'top': 1003, 'value': 'Receiver', 'valueShow': '', 'valueID': 52}, {'bottom': 1056, 'left': 1290, 'right': 1368, 'top': 1003, 'value': 'T/T', 'valueShow': '', 'valueID': 53}, {'bottom': 1047, 'left': 1390, 'right': 1486, 'top': 994, 'value': '电汇', 'valueShow': '', 'valueID': 54}, {'bottom': 1114, 'left': 92, 'right': 411, 'top': 1059, 'value': '收货人:Jamrock', 'valueShow': '', 'valueID': 55}, {'bottom': 1114, 'left': 421, 'right': 550, 'top': 1068, 'value': 'Traders', 'valueShow': '', 'valueID': 56}, {'bottom': 1114, 'left': 560, 'right': 618, 'top': 1068, 'value': 'Ltd', 'valueShow': '', 'valueID': 57}, {'bottom': 1122, 'left': 1669, 'right': 1862, 'top': 1069, 'value': '装运港:', 'valueShow': '', 'valueID': 58}, {'bottom': 1133, 'left': 1290, 'right': 1386, 'top': 1080, 'value': 'Port', 'valueShow': '', 'valueID': 59}, {'bottom': 1133, 'left': 1399, 'right': 1444, 'top': 1080, 'value': 'of', 'valueShow': '', 'valueID': 60}, {'bottom': 1133, 'left': 1458, 'right': 1645, 'top': 1080, 'value': 'Loading', 'valueShow': '', 'valueID': 61}, {'bottom': 1176, 'left': 92, 'right': 328, 'top': 1130, 'value': 'Address:Shop', 'valueShow': '', 'valueID': 62}, {'bottom': 1176, 'left': 339, 'right': 360, 'top': 1130, 'value': '7', 'valueShow': '', 'valueID': 63}, {'bottom': 1176, 'left': 370, 'right': 426, 'top': 1130, 'value': 'Taj', 'valueShow': '', 'valueID': 64}, {'bottom': 1176, 'left': 437, 'right': 544, 'top': 1130, 'value': 'Mahal', 'valueShow': '', 'valueID': 65}, {'bottom': 1176, 'left': 555, 'right': 645, 'top': 1130, 'value': 'Plaza', 'valueShow': '', 'valueID': 66}, {'bottom': 1176, 'left': 656, 'right': 747, 'top': 1130, 'value': 'Ocho', 'valueShow': '', 'valueID': 67}, {'bottom': 1176, 'left': 758, 'right': 834, 'top': 1130, 'value': 'Rios', 'valueShow': '', 'valueID': 68}, {'bottom': 1176, 'left': 846, 'right': 980, 'top': 1130, 'value': 'Jamaica', 'valueShow': '', 'valueID': 69}, {'bottom': 1211, 'left': 1338, 'right': 1453, 'top': 1158, 'value': 'China', 'valueShow': '', 'valueID': 70}, {'bottom': 1238, 'left': 92, 'right': 255, 'top': 1192, 'value': 'Receivor:', 'valueShow': '', 'valueID': 71}, {'bottom': 1238, 'left': 266, 'right': 378, 'top': 1192, 'value': 'Bobby', 'valueShow': '', 'valueID': 72}, {'bottom': 1238, 'left': 389, 'right': 562, 'top': 1192, 'value': 'Thakurani', 'valueShow': '', 'valueID': 73}, {'bottom': 1277, 'left': 1290, 'right': 1378, 'top': 1224, 'value': 'H.S.', 'valueShow': '', 'valueID': 74}, {'bottom': 1277, 'left': 1390, 'right': 1497, 'top': 1224, 'value': 'Code', 'valueShow': '', 'valueID': 75}, {'bottom': 1277, 'left': 1509, 'right': 1718, 'top': 1215, 'value': '海关编码:', 'valueShow': '', 'valueID': 76}, {'bottom': 1301, 'left': 92, 'right': 160, 'top': 1255, 'value': 'Tel:', 'valueShow': '', 'valueID': 77}, {'bottom': 1301, 'left': 171, 'right': 381, 'top': 1255, 'value': '8768225000', 'valueShow': '', 'valueID': 78}, {'bottom': 1339, 'left': 1290, 'right': 1530, 'top': 1286, 'value': '3822001090', 'valueShow': '', 'valueID': 79}, {'bottom': 1491, 'left': 905, 'right': 1239, 'top': 1438, 'value': 'DESCRIPTION', 'valueShow': '', 'valueID': 80}, {'bottom': 1491, 'left': 1251, 'right': 1317, 'top': 1438, 'value': 'OF', 'valueShow': '', 'valueID': 81}, {'bottom': 1491, 'left': 1328, 'right': 1501, 'top': 1438, 'value': 'GOODS', 'valueShow': '', 'valueID': 82}, {'bottom': 1544, 'left': 1106, 'right': 1299, 'top': 1491, 'value': '产品描述', 'valueShow': '', 'valueID': 83}, {'bottom': 1662, 'left': 2115, 'right': 2298, 'top': 1610, 'value': 'VOLUM', 'valueShow': '', 'valueID': 84}, {'bottom': 1694, 'left': 1339, 'right': 1485, 'top': 1641, 'value': 'Carton', 'valueShow': '', 'valueID': 85}, {'bottom': 1694, 'left': 1891, 'right': 2011, 'top': 1641, 'value': 'Gross', 'valueShow': '', 'valueID': 86}, {'bottom': 1725, 'left': 204, 'right': 369, 'top': 1672, 'value': 'Product', 'valueShow': '', 'valueID': 87}, {'bottom': 1725, 'left': 381, 'right': 492, 'top': 1672, 'value': 'name', 'valueShow': '', 'valueID': 88}, {'bottom': 1725, 'left': 697, 'right': 801, 'top': 1672, 'value': 'QTY', 'valueShow': '', 'valueID': 89}, {'bottom': 1725, 'left': 958, 'right': 1104, 'top': 1672, 'value': 'Carton', 'valueShow': '', 'valueID': 90}, {'bottom': 1725, 'left': 1117, 'right': 1199, 'top': 1672, 'value': 'Size', 'valueShow': '', 'valueID': 91}, {'bottom': 1725, 'left': 1567, 'right': 1639, 'top': 1672, 'value': 'Net', 'valueShow': '', 'valueID': 92}, {'bottom': 1725, 'left': 1650, 'right': 1797, 'top': 1672, 'value': 'Weight', 'valueShow': '', 'valueID': 93}, {'bottom': 1725, 'left': 2191, 'right': 2223, 'top': 1672, 'value': 'E', 'valueShow': '', 'valueID': 94}, {'bottom': 1756, 'left': 1383, 'right': 1441, 'top': 1703, 'value': 'No', 'valueShow': '', 'valueID': 95}, {'bottom': 1756, 'left': 1878, 'right': 2025, 'top': 1703, 'value': 'Weight', 'valueShow': '', 'valueID': 96}, {'bottom': 1779, 'left': 252, 'right': 444, 'top': 1726, 'value': '产品品名', 'valueShow': '', 'valueID': 97}, {'bottom': 1779, 'left': 700, 'right': 797, 'top': 1726, 'value': '数量', 'valueShow': '', 'valueID': 98}, {'bottom': 1779, 'left': 966, 'right': 1158, 'top': 1726, 'value': '包装尺寸', 'valueShow': '', 'valueID': 99}, {'bottom': 1779, 'left': 1633, 'right': 1730, 'top': 1726, 'value': '净重', 'valueShow': '', 'valueID': 100}, {'bottom': 1779, 'left': 2159, 'right': 2255, 'top': 1726, 'value': '体积', 'valueShow': '', 'valueID': 101}, {'bottom': 1810, 'left': 1364, 'right': 1460, 'top': 1757, 'value': '箱数', 'valueShow': '', 'valueID': 102}, {'bottom': 1810, 'left': 1903, 'right': 1999, 'top': 1757, 'value': '毛重', 'valueShow': '', 'valueID': 103}, {'bottom': 1828, 'left': 2246, 'right': 2261, 'top': 1794, 'value': '3', 'valueShow': '', 'valueID': 104}, {'bottom': 1850, 'left': 2185, 'right': 2246, 'top': 1797, 'value': '(M', 'valueShow': '', 'valueID': 105}, {'bottom': 1850, 'left': 2261, 'right': 2277, 'top': 1797, 'value': ')', 'valueShow': '', 'valueID': 106}, {'bottom': 1914, 'left': 177, 'right': 374, 'top': 1861, 'value': 'Urinalysis', 'valueShow': '', 'valueID': 107}, {'bottom': 1914, 'left': 386, 'right': 543, 'top': 1861, 'value': 'Reagent', 'valueShow': '', 'valueID': 108}, {'bottom': 1976, 'left': 292, 'right': 404, 'top': 1923, 'value': 'Strips', 'valueShow': '', 'valueID': 109}, {'bottom': 1976, 'left': 700, 'right': 797, 'top': 1923, 'value': '1500', 'valueShow': '', 'valueID': 110}, {'bottom': 2007, 'left': 953, 'right': 1203, 'top': 1954, 'value': '55*27*30cm', 'valueShow': '', 'valueID': 111}, {'bottom': 2007, 'left': 1313, 'right': 1361, 'top': 1954, 'value': '15', 'valueShow': '', 'valueID': 112}, {'bottom': 2007, 'left': 1373, 'right': 1511, 'top': 1954, 'value': 'cartons', 'valueShow': '', 'valueID': 113}, {'bottom': 2008, 'left': 1645, 'right': 1718, 'top': 1955, 'value': '170', 'valueShow': '', 'valueID': 114}, {'bottom': 2008, 'left': 1915, 'right': 1987, 'top': 1955, 'value': '180', 'valueShow': '', 'valueID': 115}, {'bottom': 2007, 'left': 2164, 'right': 2248, 'top': 1954, 'value': '0.67', 'valueShow': '', 'valueID': 116}, {'bottom': 2031, 'left': 180, 'right': 516, 'top': 1978, 'value': '尿液分析试纸条', 'valueShow': '', 'valueID': 117}, {'bottom': 2038, 'left': 683, 'right': 814, 'top': 1986, 'value': 'barrels', 'valueShow': '', 'valueID': 118}, {'bottom': 2101, 'left': 269, 'right': 427, 'top': 2048, 'value': 'URS-10', 'valueShow': '', 'valueID': 119}, {'bottom': 2165, 'left': 177, 'right': 374, 'top': 2112, 'value': 'Urinalysis', 'valueShow': '', 'valueID': 120}, {'bottom': 2165, 'left': 386, 'right': 543, 'top': 2112, 'value': 'Reagent', 'valueShow': '', 'valueID': 121}, {'bottom': 2228, 'left': 292, 'right': 404, 'top': 2175, 'value': 'Strips', 'valueShow': '', 'valueID': 122}, {'bottom': 2228, 'left': 700, 'right': 797, 'top': 2175, 'value': '2500', 'valueShow': '', 'valueID': 123}, {'bottom': 2259, 'left': 953, 'right': 1203, 'top': 2206, 'value': '49*49*26cm', 'valueShow': '', 'valueID': 124}, {'bottom': 2259, 'left': 1328, 'right': 1496, 'top': 2206, 'value': '13carton', 'valueShow': '', 'valueID': 125}, {'bottom': 2260, 'left': 1645, 'right': 1718, 'top': 2207, 'value': '165', 'valueShow': '', 'valueID': 126}, {'bottom': 2260, 'left': 1915, 'right': 1987, 'top': 2207, 'value': '175', 'valueShow': '', 'valueID': 127}, {'bottom': 2259, 'left': 2153, 'right': 2260, 'top': 2206, 'value': '0.811', 'valueShow': '', 'valueID': 128}, {'bottom': 2282, 'left': 180, 'right': 516, 'top': 2229, 'value': '尿液分析试纸条', 'valueShow': '', 'valueID': 129}, {'bottom': 2290, 'left': 683, 'right': 814, 'top': 2237, 'value': 'barrels', 'valueShow': '', 'valueID': 130}, {'bottom': 2353, 'left': 281, 'right': 415, 'top': 2300, 'value': 'URS-5', 'valueShow': '', 'valueID': 131}, {'bottom': 2430, 'left': 700, 'right': 797, 'top': 2377, 'value': '4000', 'valueShow': '', 'valueID': 132}, {'bottom': 2454, 'left': 276, 'right': 420, 'top': 2401, 'value': '合计:', 'valueShow': '', 'valueID': 133}, {'bottom': 2461, 'left': 1313, 'right': 1361, 'top': 2408, 'value': '28', 'valueShow': '', 'valueID': 134}, {'bottom': 2461, 'left': 1373, 'right': 1511, 'top': 2408, 'value': 'cartons', 'valueShow': '', 'valueID': 135}, {'bottom': 2462, 'left': 1645, 'right': 1718, 'top': 2409, 'value': '335', 'valueShow': '', 'valueID': 136}, {'bottom': 2462, 'left': 1915, 'right': 1987, 'top': 2409, 'value': '355', 'valueShow': '', 'valueID': 137}, {'bottom': 2461, 'left': 2123, 'right': 2290, 'top': 2408, 'value': '1.5CBM', 'valueShow': '', 'valueID': 138}, {'bottom': 2492, 'left': 683, 'right': 814, 'top': 2440, 'value': 'barrels', 'valueShow': '', 'valueID': 139}, {'bottom': 2633, 'left': 1583, 'right': 2260, 'top': 2580, 'value': 'Signature:____________________', 'valueShow': '', 'valueID': 140}], 'file_width': 2381}

regex failed

test.py file

from jsonpath import jsonpath
import json
with open(r"./test.json","r") as f:
    date=f.read()
    date=json.loads(date)
result=jsonpath(date,'$..book[?(@.price <= $["expensive"])]')
print(result)

test.json file

{
    "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
        }
    },
    "expensive": 10
}

The above test.py files return false, but the following addresses have return values
jsonpath_test_url
I hope you can support me. Thank you very much

JSONPATH.pm throws error in Perl 5.24

In file, JSONPATH.pm I am getting error below:

Unescaped left brace in regex is deprecated, passed through in regex; marked by <-- HERE in m/$obj->{ <-- HERE (.?)}(.)/ at jsonpath-perl/lib/JSONPath.pm line 302.

Looks like the error is here:
$loc =~s/$obj->{(.?)}(.)/$obj $2/;

$..book[?(@.ratings><'good')] didn't work

I have tried to use expression $..book[?(@.ratings><'good')] , since there is no ratings element in original dict, so I put one in it, the new dict is :

d = { "store": {
	"book": [
	  { "category": "reference",
		"author": "Nigel Rees",
		"title": "Sayings of the Century",
		"price": 8.95
	  },
	  { "category": "fiction",
		"author": "Evelyn Waugh",
		"title": "Sword of Honour,isbn",
		"isbnd": 'ksjd',
		"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,
		"ratings": 'good whether'   # here is ratings
	  }
	],
	"bicycle": {
	  "color": "red",
	  "price": 19.95
	}
  }
}

After that, I tried to parse the json data with jsonpath :

from jsonpath import jsonpath

print(jsonpath(d, "$..book[?(@.ratings>< 'good')]"))

However, the result is False, I also tried print(jsonpath(d, "$..book[?(@.ratings >< 'good')]")) which didn't work either.
Could you please tell me why, appreciate it a lot. :)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.