JSON Path Parse
Json Path Parser parses a Json path into an abstract syntax tree, ready to be interpreted for matching them to json data objects.
From the moment a path expression contains an operator who returns a list, result will be a list, even if only a single object matches.
input | output |
---|---|
a:b when b is list |
List |
a:b when b is object |
Single (object) |
a:b when b is neither list or object |
Single (value) |
a[] |
List |
a[1] |
List |
a[1,2] |
List |
b{} |
List |
b{prop} |
List |
b{$keys$} |
Single (object) |
{':', [a]}
is equal in meaning to {':', [{'{}', '_', a}]}
, and should internally be treated as such.firstChild
, keys
, values
)path expression | parsed form |
---|---|
<<"a{}[]">> |
{'[]',{'{}',<<"a">>,[][]} |
<<"a[1,2,3]">> |
{'[]',<<"a">>,[1,2,3]} |
<<"a{x,y,z}">> |
{'{}',<<"a">>,[<<"x">>,<<"y">>,<<"z">>]} |
<<"a:b[]:c">> |
{':',<<"c">>,{'[]',{':',<<"b">>,<<"a">>[]}} |
<<"a:b[1,2,3]">> |
{'[]',{':',<<"b">>,<<"a">>[1,2,3]} |
<<"a:b{x,y,z}">> |
{'{}',{':',<<"b">>,<<"a">>[<<"x">>,<<"y">>,<<"z">>]} |
<<"a[]{}:b{}[]">> |
{'[]',{'{}',{':',<<"b">>,{'{}',{'[]',<<"a">>,[][]}[][]} |
<<"a{$tok$}">> |
{'{}',<<"a">>,[{'$',<<"tok">>}]} |
<<"a:b{$tok$}[1,2]">> |
{'[]',{'{}',{':',<<"b">>,<<"a">>[{'$',<<"tok">>}][1,2]} |
<<"a{f(x)}[f(y)]">> |
{'[]',{'{}',<<"a">>,[{'fun',<<"f">>,[<<"x">>]}][{'fun',<<"f">>,[<<"y">>]}]} |
<<"a:b[f()]:c">> |
{':',<<"c">>,{'[]',{':',<<"b">>,<<"a">>},[{'fun',<<"f">>,[]}]}} |
<<"a:b{f()}:c">> |
{':',<<"c">>,{'{}',{':',<<"b">>,<<"a">>},[{'fun',<<"f">>,[]}]}} |
<<"a:f(y(p,q),x):c">> |
{':',<<"c">>,{':',{'fun',<<"f">>,[{'fun',<<"y">>,[<<"p">>,<<"q">>]<<"x">>]},<<"a">>}} |
<<"a:b[]{f(x:y)}:c">> |
{':',<<"c">>,{'{}',{'[]',{':',<<"b">>,<<"a">>},[]},[{'fun',<<"f">>,[{':',<<"y">>,<<"x">>}]}]}} |
1> jpparse:parsetree_with_tokens("a:b").
{ok,{{':',<<"b">>,<<"a">>},
[{'STRING',1,"a"},{':',1},{'STRING',1,"b"}]}}
2> jpparse:parsetree("a:b{1-2").
{parse_error,{1,"syntax error before: '-'",
[{'STRING',1,"a"},
{':',1},
{'STRING',1,"b"},
{'{',1},
{'STRING',1,"1"},
{'-',1},
{'STRING',1,"2"}]}}
3> jpparse:parsetree("a:b[1-2").
{parse_error,{1,"syntax error before: '-'",
[{'STRING',1,"a"},
{':',1},
{'STRING',1,"b"},
{'[',1},
{'STRING',1,"1"},
{'-',1},
{'STRING',1,"2"}]}}
4> jpparse:parsetree_with_tokens("a:b[1,2]").
{ok,{{'[]',{':',<<"b">>,<<"a">>},[1,2]},
[{'STRING',1,"a"},
{':',1},
{'STRING',1,"b"},
{'[',1},
{'STRING',1,"1"},
{',',1},
{'STRING',1,"2"},
{']',1}]}}
5> jpparse:parsetree_with_tokens("a:b{x,y}").
{ok,{{'{}',{':',<<"b">>,<<"a">>},[<<"x">>,<<"y">>]},
[{'STRING',1,"a"},
{':',1},
{'STRING',1,"b"},
{'{',1},
{'STRING',1,"x"},
{',',1},
{'STRING',1,"y"},
{'}',1}]}}