Skip to content

Instantly share code, notes, and snippets.

@josephpconley
Last active August 29, 2015 13:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save josephpconley/10647739 to your computer and use it in GitHub Desktop.
Save josephpconley/10647739 to your computer and use it in GitHub Desktop.
Scala worksheet for play-jsonpath library
//Scala worksheet
import com.josephpconley.jsonpath.JSONPath
import play.api.libs.json.Json
val store = Json.parse("""{"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}}}""")
JSONPath.query("$.store.book[*].author", store)
JSONPath.query("$..author", store)
JSONPath.query("$.store.*", store)
JSONPath.query("$.store..price", store)
JSONPath.query("$..book[2]", store)
JSONPath.query("$..book[-1]", store)
JSONPath.query("$..book[0,1]", store)
JSONPath.query("$..book[:2]", store)
JSONPath.query("$..book[?(@.isbn)]", store)
JSONPath.query("$..book[?(@.price<10)]", store)
JSONPath.query("$..*", store)
//output
store: play.api.libs.json.JsValue = {"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}}}
res0: play.api.libs.json.JsValue = ["Nigel Rees","Evelyn Waugh","Herman Melville","J. R. R. Tolkien"]
res1: play.api.libs.json.JsValue = ["Nigel Rees","Evelyn Waugh","Herman Melville","J. R. R. Tolkien"]
res2: play.api.libs.json.JsValue = [[{"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}]
res3: play.api.libs.json.JsValue = [8.95,12.99,8.99,22.99,19.95]
res4: play.api.libs.json.JsValue = {"category":"fiction","author":"Herman Melville","title":"Moby Dick","isbn":"0-553-21311-3","price":8.99}
res5: play.api.libs.json.JsValue = {"category":"fiction","author":"J. R. R. Tolkien","title":"The Lord of the Rings","isbn":"0-395-19395-8","price":22.99}
res6: play.api.libs.json.JsValue = [{"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}]
res7: play.api.libs.json.JsValue = [{"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}]
res8: play.api.libs.json.JsValue = [{"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}]
res9: play.api.libs.json.JsValue = [{"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}]
res10: play.api.libs.json.JsValue = {"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}}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment