A simple json-path parser for scala
...
The core functionality exposed in jp
is json path parsing
import jp._
val path = Path("$.store.book[0].title")
Will return a Path
object from which you may call result
to get the
corresponding ParseResult.
To make results more useful in applications, jp
exposes a trait called Transform[I,O]
which transforms a type I
into type O
given the json path parse results.
Below is an example of transforming a json path parse result with lift json.
import net.liftweb.json.JsonAST
import net.liftweb.json.Printer
path("""{"store":"book":[{"title":"test"}]}""")(as.lift.Json).fold(identity, { js =>
Printer.pretty(JsonAST.render(js))
})
Passing in a Transform
will result in Either[String, T]
where String contains an error
message for a potential malform parsing result, and T
represents the type of the transformation,
in this, a case JValue
.
JSONPath Operator | Description |
---|---|
$ | the root object/element |
@ | the current object/element |
. or [] | child operator |
.. | recursive descent. JSONPath borrows this syntax from E4X |
* | wildcard. All objects/elements regardless their names. |
[] | subscript operator. |
[,] | Union operator in XPath results in a combination of node sets. JSONPath allows alternate names or array indices as a set. |
[start:end:step] | array slice operator borrowed from ES4 |
?() | applies a filter (script) expression |
() | script expression, using the underlying script engine |
(<expr>)
expression evaluation$.store.book[(@.length-1)]
@
current object references (only useful in(<expr>)
contexts)?(<bool expr>)
filters$.store.book[?(@.price < 10)].title
[,]
union operator$..book[0,1]
Doug Tangren (softprops) 2012