Skip to content

Commit 6e7e5ef

Browse files
committed
JSONQ - JSON query utility.
1 parent 94789f0 commit 6e7e5ef

File tree

6 files changed

+412
-2
lines changed

6 files changed

+412
-2
lines changed

Emakefile

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{'src/*',
2+
[{outdir, "ebin"},
3+
compressed,
4+
debug_info,
5+
strip
6+
]}.

Makefile

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/usr/bin/make -f
2+
3+
BEAMFILES := $(wildcard ebin/*.beam) $(wildcard test/*.beam)
4+
5+
all: build
6+
7+
build: ebin/$(APPFILE)
8+
erl -make
9+
10+
clean:
11+
-rm -rf ebin/$(APPFILE) $(BEAMFILES)
12+
13+
test: build
14+
erlc -W +debug_info +compressed +strip -o test/ test/*.erl
15+
erl -noshell -pa ebin -pa test -eval "query_tests:test()" -eval "init:stop()"

README.md

+117-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,117 @@
1-
jsonq
2-
=====
1+
Querying JSON Terms
2+
-------------------
3+
4+
JSON:
5+
6+
{"store": {
7+
"books": [
8+
{
9+
"category": "fiction",
10+
"author": "J. R. R. Tolkien",
11+
"title": "The Lord of the Rings",
12+
"isbn": "0-395-19395-8",
13+
"price": 22.99
14+
},
15+
{
16+
"category": "fiction",
17+
"author": ["Arkady N. Strugatsky", "Boris N. Strugatsky"],
18+
"title": "Monday Begins on Saturday",
19+
"isbn": "0-000-000000-0",
20+
"price": 22.99
21+
}
22+
],
23+
"bicycle": {
24+
"color": "red",
25+
"price": 19.95
26+
}
27+
}}
28+
29+
Erlang JSON term:
30+
31+
Json = {[{<<"store">>,
32+
{[{<<"books">>, [
33+
{[
34+
{<<"category">>,<<"fiction">>},
35+
{<<"author">>,<<"J. R. R. Tolkien">>},
36+
{<<"title">>,<<"The Lord of the Rings">>},
37+
{<<"isbn">>,<<"0-395-19395-8">>},
38+
{<<"price">>,22.99}
39+
]},
40+
{[
41+
{<<"category">>,<<"fiction">>},
42+
{<<"author">>, [<<"Arkady N. Strugatsky">>,<<"Boris N. Strugatsky">>]},
43+
{<<"title">>,<<"Monday Begins on Saturday">>},
44+
{<<"isbn">>,<<"0-000-000000-0">>},
45+
{<<"price">>,22.99}
46+
]}
47+
]},
48+
{<<"bicycle">>, {[
49+
{<<"color">>,<<"red">>},
50+
{<<"price">>,19.95}
51+
]}
52+
}]}}]}.
53+
54+
Query example 1:
55+
56+
jsonq:q([<<"store">>], Json).
57+
58+
{[{<<"books">>, [
59+
{[
60+
{<<"category">>,<<"fiction">>},
61+
{<<"author">>,<<"J. R. R. Tolkien">>},
62+
{<<"title">>,<<"The Lord of the Rings">>},
63+
{<<"isbn">>,<<"0-395-19395-8">>},
64+
{<<"price">>,22.99}
65+
]},
66+
{[
67+
{<<"category">>,<<"fiction">>},
68+
{<<"author">>, [<<"Arkady N. Strugatsky">>,<<"Boris N. Strugatsky">>]},
69+
{<<"title">>,<<"Monday Begins on Saturday">>},
70+
{<<"isbn">>,<<"0-000-000000-0">>},
71+
{<<"price">>,22.99}
72+
]}
73+
]}
74+
75+
Query example 2:
76+
77+
jsonq:q([<<"store">>, <<"books">>], Json).
78+
[
79+
{[
80+
{<<"category">>,<<"fiction">>},
81+
{<<"author">>,<<"J. R. R. Tolkien">>},
82+
{<<"title">>,<<"The Lord of the Rings">>},
83+
{<<"isbn">>,<<"0-395-19395-8">>},
84+
{<<"price">>,22.99}
85+
]},
86+
{[
87+
{<<"category">>,<<"fiction">>},
88+
{<<"author">>, [<<"Arkady N. Strugatsky">>,<<"Boris N. Strugatsky">>]},
89+
{<<"title">>,<<"Monday Begins on Saturday">>},
90+
{<<"isbn">>,<<"0-000-000000-0">>},
91+
{<<"price">>,22.99}
92+
]}
93+
]
94+
95+
Query example 3:
96+
97+
jsonq:q([<<"store">>, <<"books">>, 0], Json).
98+
99+
{[
100+
{<<"category">>,<<"fiction">>},
101+
{<<"author">>,<<"J. R. R. Tolkien">>},
102+
{<<"title">>,<<"The Lord of the Rings">>},
103+
{<<"isbn">>,<<"0-395-19395-8">>},
104+
{<<"price">>,22.99}
105+
]}
106+
107+
Query example 4:
108+
109+
jsonq:q([<<"store">>, <<"books">>, 1, <<"author">>, 1], Json).
110+
111+
<<"Boris N. Strugatsky">>
112+
113+
Query example 5:
114+
115+
jsonq:q([<<"store">>, <<"toys">>], Json).
116+
117+
undefined

rebar.config

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{erl_opts, [warnings_as_errors]}.

src/jsonq.erl

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
-module(jsonq).
2+
3+
-export([q/2]).
4+
5+
q(_, undefined) ->
6+
undefined;
7+
q([], Obj) ->
8+
Obj;
9+
q([Key | Rest], {Obj}) ->
10+
q(Rest, proplists:get_value(Key, Obj));
11+
q([Index | _Rest], []) when is_integer(Index) ->
12+
undefined;
13+
q([Index | Rest], Obj) when is_integer(Index), is_list(Obj) ->
14+
case Index < length(Obj) of
15+
true -> q(Rest, lists:nth(Index + 1, Obj));
16+
false -> undefined
17+
end;
18+
q([Key | Rest], Obj) when is_list(Obj) ->
19+
q(Rest, proplists:get_all_values(Key, Obj)).

0 commit comments

Comments
 (0)