README 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. -------------------------------------------------------------------------------
  2. dojox.jsonPath
  3. -------------------------------------------------------------------------------
  4. Version 1.0
  5. Release date: 11/14/2007
  6. -------------------------------------------------------------------------------
  7. Project state: beta
  8. -------------------------------------------------------------------------------
  9. Project authors
  10. Dustin Machi
  11. Kris Zyp
  12. -------------------------------------------------------------------------------
  13. Project description
  14. jsonPath is a query system similar in idea to xpath, but for use against
  15. javascript objects. This code is a port of the jsonPath code at
  16. http://code.google.com/p/jsonpath/. It was contributed under CLA by Stefan
  17. Goessner. Thanks Stefan!
  18. -------------------------------------------------------------------------------
  19. Dependencies:
  20. Dojo Core (package loader).
  21. -------------------------------------------------------------------------------
  22. Documentation
  23. Usage:
  24. var matches = dojox.jsonPath.query(objectToQuery, jsonPathExpresson)
  25. Expressions:
  26. $ The Root Object
  27. @ The current object/element
  28. . or [] The child operator
  29. .. Recursive descent
  30. * all objects
  31. [] subscript operator
  32. [,] Union operator
  33. [start:end:step] array slice operator
  34. ?() applies a filter/script expression
  35. () script expresions
  36. some examples:
  37. Given the following test data set:
  38. var json =
  39. { "store": {
  40. "book": [
  41. { "category": "reference",
  42. "author": "Nigel Rees",
  43. "title": "Sayings of the Century",
  44. "price": 8.95
  45. },
  46. { "category": "fiction",
  47. "author": "Evelyn Waugh",
  48. "title": "Sword of Honour",
  49. "price": 12.99
  50. },
  51. { "category": "fiction",
  52. "author": "Herman Melville",
  53. "title": "Moby Dick",
  54. "isbn": "0-553-21311-3",
  55. "price": 8.99
  56. },
  57. { "category": "fiction",
  58. "author": "J. R. R. Tolkien",
  59. "title": "The Lord of the Rings",
  60. "isbn": "0-395-19395-8",
  61. "price": 22.99
  62. }
  63. ],
  64. "bicycle": {
  65. "color": "red",
  66. "price": 19.95
  67. }
  68. }
  69. };
  70. Here are some example queries and their output:
  71. $.store.book[*].author
  72. ["Nigel Rees","Evelyn Waugh","Herman Melville","J. R. R. Tolkien"]
  73. $..author
  74. ["Nigel Rees","Evelyn Waugh","Herman Melville","J. R. R. Tolkien"]
  75. $.store.*
  76. [[{"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}]
  77. $.store..price
  78. [8.95,12.99,8.99,22.99,19.95]
  79. $..book[(@.length-1)]
  80. [{"category":"fiction","author":"J. R. R. Tolkien","title":"The Lord of the Rings","isbn":"0-395-19395-8","price":22.99}]
  81. $..book[-1]
  82. [{"category":"fiction","author":"J. R. R. Tolkien","title":"The Lord of the Rings","isbn":"0-395-19395-8","price":22.99}]
  83. $..book[0,1]
  84. [{"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}]
  85. $..book[:2]
  86. [{"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}]
  87. $..book[?(@.isbn)]
  88. [{"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}]
  89. $..book[?(@.price<10)]
  90. [{"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}]
  91. $..*
  92. [{"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]
  93. -------------------------------------------------------------------------------
  94. Installation instructions
  95. Grab the following from the Dojo SVN Repository:
  96. http://svn.dojotoolkit.org/var/src/dojo/dojox/trunk/jsonPath
  97. Install into the following directory structure:
  98. /dojox/jsonPath/
  99. ...which should be at the same level as your Dojo checkout.