127 lines
6.5 KiB
Text
127 lines
6.5 KiB
Text
|
-------------------------------------------------------------------------------
|
||
|
dojox.jsonPath
|
||
|
-------------------------------------------------------------------------------
|
||
|
Version 1.0
|
||
|
Release date: 11/14/2007
|
||
|
-------------------------------------------------------------------------------
|
||
|
Project state: beta
|
||
|
-------------------------------------------------------------------------------
|
||
|
Project authors
|
||
|
Dustin Machi
|
||
|
Kris Zyp
|
||
|
-------------------------------------------------------------------------------
|
||
|
Project description
|
||
|
|
||
|
jsonPath is a query system similar in idea to xpath, but for use against
|
||
|
javascript objects. This code is a port of the jsonPath code at
|
||
|
http://code.google.com/p/jsonpath/. It was contributed under CLA by Stefan
|
||
|
Goessner. Thanks Stefan!
|
||
|
|
||
|
-------------------------------------------------------------------------------
|
||
|
Dependencies:
|
||
|
|
||
|
Dojo Core (package loader).
|
||
|
-------------------------------------------------------------------------------
|
||
|
Documentation
|
||
|
|
||
|
Usage:
|
||
|
|
||
|
var matches = dojox.jsonPath.query(objectToQuery, jsonPathExpresson)
|
||
|
|
||
|
Expressions:
|
||
|
|
||
|
$ The Root Object
|
||
|
@ The current object/element
|
||
|
. or [] The child operator
|
||
|
.. Recursive descent
|
||
|
* all objects
|
||
|
[] subscript operator
|
||
|
[,] Union operator
|
||
|
[start:end:step] array slice operator
|
||
|
?() applies a filter/script expression
|
||
|
() script expresions
|
||
|
|
||
|
some examples:
|
||
|
|
||
|
Given the following test data set:
|
||
|
|
||
|
var json =
|
||
|
{ "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 some example queries and their output:
|
||
|
|
||
|
$.store.book[*].author
|
||
|
["Nigel Rees","Evelyn Waugh","Herman Melville","J. R. R. Tolkien"]
|
||
|
|
||
|
$..author
|
||
|
["Nigel Rees","Evelyn Waugh","Herman Melville","J. R. R. Tolkien"]
|
||
|
|
||
|
$.store.*
|
||
|
[[{"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}],{"color":"red","price":19.95}]
|
||
|
|
||
|
$.store..price
|
||
|
[8.95,12.99,8.99,22.99,19.95]
|
||
|
|
||
|
$..book[(@.length-1)]
|
||
|
[{"category":"fiction","author":"J. R. R. Tolkien","title":"The Lord of the Rings","isbn":"0-395-19395-8","price":22.99}]
|
||
|
|
||
|
$..book[-1]
|
||
|
[{"category":"fiction","author":"J. R. R. Tolkien","title":"The Lord of the Rings","isbn":"0-395-19395-8","price":22.99}]
|
||
|
|
||
|
$..book[0,1]
|
||
|
[{"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}]
|
||
|
|
||
|
$..book[:2]
|
||
|
[{"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}]
|
||
|
|
||
|
$..book[?(@.isbn)]
|
||
|
[{"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}]
|
||
|
|
||
|
$..book[?(@.price<10)]
|
||
|
[{"category":"reference","author":"Nigel Rees","title":"Sayings of the Century","price":8.95},{"category":"fiction","author":"Herman Melville","title":"Moby Dick","isbn":"0-553-21311-3","price":8.99}]
|
||
|
|
||
|
$..*
|
||
|
[{"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}},[{"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}],{"color":"red","price":19.95},{"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},"reference","Nigel Rees","Sayings of the Century",8.95,"fiction","Evelyn Waugh","Sword of Honour",12.99,"fiction","Herman Melville","Moby Dick","0-553-21311-3",8.99,"fiction","J. R. R. Tolkien","The Lord of the Rings","0-395-19395-8",22.99,"red",19.95]
|
||
|
|
||
|
|
||
|
-------------------------------------------------------------------------------
|
||
|
Installation instructions
|
||
|
|
||
|
Grab the following from the Dojo SVN Repository:
|
||
|
http://svn.dojotoolkit.org/var/src/dojo/dojox/trunk/jsonPath
|
||
|
|
||
|
Install into the following directory structure:
|
||
|
/dojox/jsonPath/
|
||
|
|
||
|
...which should be at the same level as your Dojo checkout.
|