1 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
4 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
5 <title>JsonPath</title>
6 <meta name="generator" content="DocBook XSL Stylesheets V1.76.1">
7 <link rel="home" href="index.html" title="JSON-GLib Reference Manual">
8 <link rel="up" href="ch02.html" title="Parser">
9 <link rel="prev" href="JsonReader.html" title="JsonReader">
10 <link rel="next" href="ch03.html" title="Generator">
11 <meta name="generator" content="GTK-Doc V1.18.1 (XML mode)">
12 <link rel="stylesheet" href="style.css" type="text/css">
14 <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
15 <table class="navigation" id="top" width="100%" summary="Navigation header" cellpadding="2" cellspacing="2">
17 <td><a accesskey="p" href="JsonReader.html"><img src="left.png" width="24" height="24" border="0" alt="Prev"></a></td>
18 <td><a accesskey="u" href="ch02.html"><img src="up.png" width="24" height="24" border="0" alt="Up"></a></td>
19 <td><a accesskey="h" href="index.html"><img src="home.png" width="24" height="24" border="0" alt="Home"></a></td>
20 <th width="100%" align="center">JSON-GLib Reference Manual</th>
21 <td><a accesskey="n" href="ch03.html"><img src="right.png" width="24" height="24" border="0" alt="Next"></a></td>
23 <tr><td colspan="5" class="shortcuts">
24 <a href="#JsonPath.synopsis" class="shortcut">Top</a>
26 <a href="#JsonPath.description" class="shortcut">Description</a>
28 <a href="#JsonPath.object-hierarchy" class="shortcut">Object Hierarchy</a>
31 <div class="refentry">
32 <a name="JsonPath"></a><div class="titlepage"></div>
33 <div class="refnamediv"><table width="100%"><tr>
35 <h2><span class="refentrytitle"><a name="JsonPath.top_of_page"></a>JsonPath</span></h2>
36 <p>JsonPath — JSONPath implementation</p>
38 <td valign="top" align="right"></td>
40 <div class="refsynopsisdiv">
41 <a name="JsonPath.synopsis"></a><h2>Synopsis</h2>
42 <pre class="synopsis"> <a class="link" href="JsonPath.html#JsonPath-struct" title="JsonPath">JsonPath</a>;
43 <a class="link" href="JsonPath.html#JsonPathClass" title="JsonPathClass">JsonPathClass</a>;
44 <a class="link" href="JsonPath.html" title="JsonPath"><span class="returnvalue">JsonPath</span></a> * <a class="link" href="JsonPath.html#json-path-new" title="json_path_new ()">json_path_new</a> (<em class="parameter"><code><span class="type">void</span></code></em>);
45 #define <a class="link" href="JsonPath.html#JSON-PATH-ERROR:CAPS" title="JSON_PATH_ERROR">JSON_PATH_ERROR</a>
46 enum <a class="link" href="JsonPath.html#JsonPathError" title="enum JsonPathError">JsonPathError</a>;
47 <span class="returnvalue">gboolean</span> <a class="link" href="JsonPath.html#json-path-compile" title="json_path_compile ()">json_path_compile</a> (<em class="parameter"><code><a class="link" href="JsonPath.html" title="JsonPath"><span class="type">JsonPath</span></a> *path</code></em>,
48 <em class="parameter"><code>const <span class="type">char</span> *expression</code></em>,
49 <em class="parameter"><code><span class="type">GError</span> **error</code></em>);
50 <a class="link" href="json-glib-JSON-Node.html#JsonNode" title="JsonNode"><span class="returnvalue">JsonNode</span></a> * <a class="link" href="JsonPath.html#json-path-match" title="json_path_match ()">json_path_match</a> (<em class="parameter"><code><a class="link" href="JsonPath.html" title="JsonPath"><span class="type">JsonPath</span></a> *path</code></em>,
51 <em class="parameter"><code><a class="link" href="json-glib-JSON-Node.html#JsonNode" title="JsonNode"><span class="type">JsonNode</span></a> *root</code></em>);
53 <a class="link" href="json-glib-JSON-Node.html#JsonNode" title="JsonNode"><span class="returnvalue">JsonNode</span></a> * <a class="link" href="JsonPath.html#json-path-query" title="json_path_query ()">json_path_query</a> (<em class="parameter"><code>const <span class="type">char</span> *expression</code></em>,
54 <em class="parameter"><code><a class="link" href="json-glib-JSON-Node.html#JsonNode" title="JsonNode"><span class="type">JsonNode</span></a> *root</code></em>,
55 <em class="parameter"><code><span class="type">GError</span> **error</code></em>);
58 <div class="refsect1">
59 <a name="JsonPath.object-hierarchy"></a><h2>Object Hierarchy</h2>
60 <pre class="synopsis">
65 <div class="refsect1">
66 <a name="JsonPath.description"></a><h2>Description</h2>
68 <a class="link" href="JsonPath.html" title="JsonPath"><span class="type">JsonPath</span></a> is a simple class implementing the JSONPath syntax for extracting
69 data out of a JSON tree. While the semantics of the JSONPath expressions are
70 heavily borrowed by the XPath specification for XML, the syntax follows the
71 ECMAScript origins of JSON.
74 Once a <a class="link" href="JsonPath.html" title="JsonPath"><span class="type">JsonPath</span></a> instance has been created, it has to compile a JSONPath
75 expression using <a class="link" href="JsonPath.html#json-path-compile" title="json_path_compile ()"><code class="function">json_path_compile()</code></a> before being able to match it to a
76 JSON tree; the same <a class="link" href="JsonPath.html" title="JsonPath"><span class="type">JsonPath</span></a> instance can be used to match multiple JSON
77 trees. It it also possible to compile a new JSONPath expression using the
78 same <a class="link" href="JsonPath.html" title="JsonPath"><span class="type">JsonPath</span></a> instance; the previous expression will be discarded only if
79 the compilation of the new expression is successful.
82 The simple convenience function <a class="link" href="JsonPath.html#json-path-query" title="json_path_query ()"><code class="function">json_path_query()</code></a> can be used for one-off
87 <div class="refsect2">
88 <a name="json-path-syntax"></a><h3>Syntax of the JSONPath expressions</h3>
89 <p>A JSONPath expression is composed by path indices and operators.
90 Each path index can either be a member name or an element index inside
91 a JSON tree. A JSONPath expression must start with the '$' operator; each
92 path index is separated using either the dot notation or the bracket
94 <div class="informalexample">
95 <table class="listing_frame" border="0" cellpadding="0" cellspacing="0">
98 <td class="listing_lines" align="right"><pre>1
102 <td class="listing_code"><pre class="programlisting"><span class="comment">/* dot notation */</span>
103 <span class="normal">$</span><span class="symbol">.</span><span class="normal">store</span><span class="symbol">.</span><span class="normal">book</span><span class="symbol">[</span><span class="number">0</span><span class="symbol">].</span><span class="normal">title</span>
104 <span class="comment">/* bracket notation */</span>
105 <span class="normal">$</span><span class="symbol">[</span><span class="string">'store'</span><span class="symbol">][</span><span class="string">'book'</span><span class="symbol">][</span><span class="number">0</span><span class="symbol">][</span><span class="string">'title'</span><span class="symbol">]</span></pre></td>
111 <p>The available operators are:</p>
113 <a name="json-path-operators"></a><p class="title"><b>Table 1. Operators</b></p>
114 <div class="table-contents"><table summary="Operators" border="1">
130 <td>The root node</td>
132 <td>The whole document</td>
136 <td>The child member or element</td>
137 <td>$.store.book</td>
138 <td>The contents of the book member of the store object</td>
142 <td>Recursive descent</td>
144 <td>The content of the author member in every object</td>
149 <td>$.store.book[*].author</td>
150 <td>The content of the author member of any object of the
151 array contained in the book member of the store object</td>
156 <td>$.store.book[0]</td>
157 <td>The first element of the array contained in the book
158 member of the store object</td>
163 <td>$.store.book[0,1]</td>
164 <td>The first two elements of the array contained in the
165 book member of the store object</td>
168 <td>[start:end:step]</td>
170 <td>$.store.book[:2]</td>
171 <td>The first two elements of the array contained in the
172 book member of the store object; the start and step are omitted
173 and implied to be 0 and 1, respectively</td>
178 <br class="table-break"><p>More information about JSONPath is available on Stefan Gössner's
179 <a class="ulink" href="http://goessner.net/articles/JsonPath/" target="_top">website</a>.</p>
185 <div class="example">
186 <a name="json-path-example"></a><p class="title"><b>Example 1. Example of JsonPath usage</b></p>
187 <div class="example-contents">
188 <p>The following example shows some of the results of using <a class="link" href="JsonPath.html" title="JsonPath"><span class="type">JsonPath</span></a>
189 on a JSON tree. We use the following JSON description of a
191 <pre class="programlisting">
194 { "category": "reference",
195 "author": "Nigel Rees",
196 "title": "Sayings of the Century",
199 { "category": "fiction",
200 "author": "Evelyn Waugh",
201 "title": "Sword of Honour",
204 { "category": "fiction",
205 "author": "Herman Melville",
206 "title": "Moby Dick",
207 "isbn": "0-553-21311-3",
210 { "category": "fiction",
211 "author": "J. R. R. Tolkien",
212 "title": "The Lord of the Rings",
213 "isbn": "0-395-19395-8",
224 <p>We can parse the JSON using <a class="link" href="JsonParser.html" title="JsonParser"><span class="type">JsonParser</span></a>:</p>
225 <pre class="programlisting">
226 JsonParser *parser = json_parser_new ();
227 json_parser_load_from_data (parser, json_data, -1, NULL);
229 <p>If we run the following code:</p>
230 <pre class="programlisting">
232 JsonPath *path = json_path_new ();
233 json_path_compile (path, "$.store..author", NULL);
234 result = json_path_match (path, json_parser_get_root (parser));
236 <p>The <span class="emphasis"><em>result</em></span> <a class="link" href="json-glib-JSON-Node.html#JsonNode" title="JsonNode"><span class="type">JsonNode</span></a> will contain an array
237 with all values of the <span class="emphasis"><em>author</em></span> member of the objects
238 in the JSON tree. If we use a <a class="link" href="JsonGenerator.html" title="JsonGenerator"><span class="type">JsonGenerator</span></a> to convert the <a class="link" href="json-glib-JSON-Node.html#JsonNode" title="JsonNode"><span class="type">JsonNode</span></a>
239 to a string and print it:</p>
240 <pre class="programlisting">
241 JsonGenerator *generator = json_generator_new ();
243 json_generator_set_pretty (generator, TRUE);
244 json_generator_set_root (generator, result);
245 str = json_generator_to_data (generator, NULL);
246 g_print ("Results: %s\n", str);
248 <p>The output will be:</p>
249 <pre class="programlisting">
259 <p><br class="example-break">
262 <a class="link" href="JsonPath.html" title="JsonPath"><span class="type">JsonPath</span></a> is available since JSON-GLib 0.14
265 <div class="refsect1">
266 <a name="JsonPath.details"></a><h2>Details</h2>
267 <div class="refsect2">
268 <a name="JsonPath-struct"></a><h3>JsonPath</h3>
269 <pre class="programlisting">typedef struct _JsonPath JsonPath;</pre>
271 The <span class="structname">JsonPath</span> structure is an opaque object
272 whose members cannot be directly accessed except through the provided
275 <p class="since">Since 0.14</p>
278 <div class="refsect2">
279 <a name="JsonPathClass"></a><h3>JsonPathClass</h3>
280 <pre class="programlisting">typedef struct _JsonPathClass JsonPathClass;</pre>
282 The <span class="structname">JsonPathClass</span> structure is an opaque
283 object class whose members cannot be directly accessed.
285 <p class="since">Since 0.14</p>
288 <div class="refsect2">
289 <a name="json-path-new"></a><h3>json_path_new ()</h3>
290 <pre class="programlisting"><a class="link" href="JsonPath.html" title="JsonPath"><span class="returnvalue">JsonPath</span></a> * json_path_new (<em class="parameter"><code><span class="type">void</span></code></em>);</pre>
292 Creates a new <a class="link" href="JsonPath.html" title="JsonPath"><span class="type">JsonPath</span></a> instance.
295 Once created, the <a class="link" href="JsonPath.html" title="JsonPath"><span class="type">JsonPath</span></a> object should be used with <a class="link" href="JsonPath.html#json-path-compile" title="json_path_compile ()"><code class="function">json_path_compile()</code></a>
296 and <a class="link" href="JsonPath.html#json-path-match" title="json_path_match ()"><code class="function">json_path_match()</code></a>.
298 <div class="variablelist"><table border="0">
299 <col align="left" valign="top">
301 <td><p><span class="term"><span class="emphasis"><em>Returns</em></span> :</span></p></td>
302 <td>the newly created <a class="link" href="JsonPath.html" title="JsonPath"><span class="type">JsonPath</span></a> instance. Use
303 <code class="function">g_object_unref()</code> to free the allocated resources when done. <span class="annotation">[<acronym title="Free data after the code is done."><span class="acronym">transfer full</span></acronym>]</span>
307 <p class="since">Since 0.14</p>
310 <div class="refsect2">
311 <a name="JSON-PATH-ERROR:CAPS"></a><h3>JSON_PATH_ERROR</h3>
312 <pre class="programlisting">#define JSON_PATH_ERROR (json_path_error_quark ())
315 Error domain for <a class="link" href="JsonPath.html" title="JsonPath"><span class="type">JsonPath</span></a> errors
317 <p class="since">Since 0.14</p>
320 <div class="refsect2">
321 <a name="JsonPathError"></a><h3>enum JsonPathError</h3>
322 <pre class="programlisting">typedef enum {
323 JSON_PATH_ERROR_INVALID_QUERY
327 Error code enumeration for the <a class="link" href="JsonPath.html#JSON-PATH-ERROR:CAPS" title="JSON_PATH_ERROR"><code class="literal">JSON_PATH_ERROR</code></a> domain.
329 <div class="variablelist"><table border="0">
330 <col align="left" valign="top">
332 <td><p><a name="JSON-PATH-ERROR-INVALID-QUERY:CAPS"></a><span class="term"><code class="literal">JSON_PATH_ERROR_INVALID_QUERY</code></span></p></td>
337 <p class="since">Since 0.14</p>
340 <div class="refsect2">
341 <a name="json-path-compile"></a><h3>json_path_compile ()</h3>
342 <pre class="programlisting"><span class="returnvalue">gboolean</span> json_path_compile (<em class="parameter"><code><a class="link" href="JsonPath.html" title="JsonPath"><span class="type">JsonPath</span></a> *path</code></em>,
343 <em class="parameter"><code>const <span class="type">char</span> *expression</code></em>,
344 <em class="parameter"><code><span class="type">GError</span> **error</code></em>);</pre>
346 Validates and decomposes <em class="parameter"><code>expression</code></em>.
349 A JSONPath expression must be compiled before calling <a class="link" href="JsonPath.html#json-path-match" title="json_path_match ()"><code class="function">json_path_match()</code></a>.
351 <div class="variablelist"><table border="0">
352 <col align="left" valign="top">
355 <td><p><span class="term"><em class="parameter"><code>path</code></em> :</span></p></td>
356 <td>a <a class="link" href="JsonPath.html" title="JsonPath"><span class="type">JsonPath</span></a>
360 <td><p><span class="term"><em class="parameter"><code>expression</code></em> :</span></p></td>
361 <td>a JSONPath expression</td>
364 <td><p><span class="term"><em class="parameter"><code>error</code></em> :</span></p></td>
365 <td>return location for a <span class="type">GError</span>, or <a href="/home/ebassi/gnome3/install/share/gtk-doc/html/liboil/liboil-liboiljunk.html#NULL--CAPS"><code class="literal">NULL</code></a>
369 <td><p><span class="term"><span class="emphasis"><em>Returns</em></span> :</span></p></td>
371 <code class="literal">TRUE</code> on success; on error, <em class="parameter"><code>error</code></em> will be set with
372 the <a class="link" href="JsonPath.html#JSON-PATH-ERROR:CAPS" title="JSON_PATH_ERROR"><code class="literal">JSON_PATH_ERROR</code></a> domain and a code from the <a class="link" href="JsonPath.html#JsonPathError" title="enum JsonPathError"><span class="type">JsonPathError</span></a>
373 enumeration, and <code class="literal">FALSE</code> will be returned</td>
377 <p class="since">Since 0.14</p>
380 <div class="refsect2">
381 <a name="json-path-match"></a><h3>json_path_match ()</h3>
382 <pre class="programlisting"><a class="link" href="json-glib-JSON-Node.html#JsonNode" title="JsonNode"><span class="returnvalue">JsonNode</span></a> * json_path_match (<em class="parameter"><code><a class="link" href="JsonPath.html" title="JsonPath"><span class="type">JsonPath</span></a> *path</code></em>,
383 <em class="parameter"><code><a class="link" href="json-glib-JSON-Node.html#JsonNode" title="JsonNode"><span class="type">JsonNode</span></a> *root</code></em>);</pre>
385 Matches the JSON tree pointed by <em class="parameter"><code>root</code></em> using the expression compiled
386 into the <a class="link" href="JsonPath.html" title="JsonPath"><span class="type">JsonPath</span></a>.
389 The matching <a class="link" href="json-glib-JSON-Node.html#JsonNode" title="JsonNode"><span class="type">JsonNode</span></a>s will be copied into a <a class="link" href="json-glib-JSON-Array.html#JsonArray" title="JsonArray"><span class="type">JsonArray</span></a> and
390 returned wrapped in a <a class="link" href="json-glib-JSON-Node.html#JsonNode" title="JsonNode"><span class="type">JsonNode</span></a>.
392 <div class="variablelist"><table border="0">
393 <col align="left" valign="top">
396 <td><p><span class="term"><em class="parameter"><code>path</code></em> :</span></p></td>
397 <td>a compiled <a class="link" href="JsonPath.html" title="JsonPath"><span class="type">JsonPath</span></a>
401 <td><p><span class="term"><em class="parameter"><code>root</code></em> :</span></p></td>
402 <td>a <a class="link" href="json-glib-JSON-Node.html#JsonNode" title="JsonNode"><span class="type">JsonNode</span></a>
406 <td><p><span class="term"><span class="emphasis"><em>Returns</em></span> :</span></p></td>
407 <td>a newly-created <a class="link" href="json-glib-JSON-Node.html#JsonNode" title="JsonNode"><span class="type">JsonNode</span></a> of type
408 <a class="link" href="json-glib-JSON-Node.html#JSON-NODE-ARRAY:CAPS"><code class="literal">JSON_NODE_ARRAY</code></a> containing an array of matching <a class="link" href="json-glib-JSON-Node.html#JsonNode" title="JsonNode"><span class="type">JsonNode</span></a>s.
409 Use <a class="link" href="json-glib-JSON-Node.html#json-node-free" title="json_node_free ()"><code class="function">json_node_free()</code></a> when done. <span class="annotation">[<acronym title="Free data after the code is done."><span class="acronym">transfer full</span></acronym>]</span>
414 <p class="since">Since 0.14</p>
417 <div class="refsect2">
418 <a name="json-path-query"></a><h3>json_path_query ()</h3>
419 <pre class="programlisting"><a class="link" href="json-glib-JSON-Node.html#JsonNode" title="JsonNode"><span class="returnvalue">JsonNode</span></a> * json_path_query (<em class="parameter"><code>const <span class="type">char</span> *expression</code></em>,
420 <em class="parameter"><code><a class="link" href="json-glib-JSON-Node.html#JsonNode" title="JsonNode"><span class="type">JsonNode</span></a> *root</code></em>,
421 <em class="parameter"><code><span class="type">GError</span> **error</code></em>);</pre>
423 Queries a JSON tree using a JSONPath expression.
426 This function is a simple wrapper around <a class="link" href="JsonPath.html#json-path-new" title="json_path_new ()"><code class="function">json_path_new()</code></a>,
427 <a class="link" href="JsonPath.html#json-path-compile" title="json_path_compile ()"><code class="function">json_path_compile()</code></a> and <a class="link" href="JsonPath.html#json-path-match" title="json_path_match ()"><code class="function">json_path_match()</code></a>. It implicitly
428 creates a <a class="link" href="JsonPath.html" title="JsonPath"><span class="type">JsonPath</span></a> instance, compiles <em class="parameter"><code>expression</code></em> and
429 matches it against the JSON tree pointed by <em class="parameter"><code>root</code></em>.
431 <div class="variablelist"><table border="0">
432 <col align="left" valign="top">
435 <td><p><span class="term"><em class="parameter"><code>expression</code></em> :</span></p></td>
436 <td>a JSONPath expression</td>
439 <td><p><span class="term"><em class="parameter"><code>root</code></em> :</span></p></td>
440 <td>the root of a JSON tree</td>
443 <td><p><span class="term"><em class="parameter"><code>error</code></em> :</span></p></td>
444 <td>return location for a <span class="type">GError</span>, or <a href="/home/ebassi/gnome3/install/share/gtk-doc/html/liboil/liboil-liboiljunk.html#NULL--CAPS"><code class="literal">NULL</code></a>
448 <td><p><span class="term"><span class="emphasis"><em>Returns</em></span> :</span></p></td>
449 <td>a newly-created <a class="link" href="json-glib-JSON-Node.html#JsonNode" title="JsonNode"><span class="type">JsonNode</span></a> of type
450 <a class="link" href="json-glib-JSON-Node.html#JSON-NODE-ARRAY:CAPS"><code class="literal">JSON_NODE_ARRAY</code></a> containing an array of matching <a class="link" href="json-glib-JSON-Node.html#JsonNode" title="JsonNode"><span class="type">JsonNode</span></a>s.
451 Use <a class="link" href="json-glib-JSON-Node.html#json-node-free" title="json_node_free ()"><code class="function">json_node_free()</code></a> when done. <span class="annotation">[<acronym title="Free data after the code is done."><span class="acronym">transfer full</span></acronym>]</span>
456 <p class="since">Since 0.14</p>
462 Generated by GTK-Doc V1.18.1</div>