<h1 id="json">JSON</h1>
<p>Json is a cool little data serialization language, that allows you to easily and clearly demarcate blocks of data by nesting data structures such as lists (enclosed by square brackets) and key-value pairs or "dictionaries" (enclosed by curly braces). So that in the end you get something that looks like this</p>
<pre><code>{
&quot;first list&quot; : [ &quot;element1&quot;, &quot;element2&quot;, {&quot;element3&quot; : &quot;is another k-v pair&quot;, &quot;but contains&quot; : [&quot;a&quot; , &quot;list&quot;, &quot;of&quot;, &quot;words&quot;]}] ,
&quot;this value is a string&quot; : &quot;1&quot; ,
&quot;and this is a number&quot; : 23 ,
&quot;and floating point&quot; :  1.413
}</code></pre>
<p>Aside from:</p>
<ul>
<li>Lists are enclosed in [] and each element is separated by ,</li>
<li>Key-value pair lists are enclosed in {} and have the key and value separated by : and each pair is separated by ,</li>
<li>Keys have to strings quoted with double quotes</li>
<li>Numbers may be left unquoted (but just in value fields)</li>
</ul>
<p>There are no restrictions to what you can do with JSON. Given how explicit the syntax is then, it makes for very easy parsing, and there are plenty of good parser out there. <a href="https://stedolan.github.io/jq/">My favourite JSON parser is jq(1)</a>.</p>
<p>A canonical representation of the JSON example above can easily be obtained with jq by simply calling <code>jq '' file.json</code> (or piping the file through stdin, or even putting the contents properly quoted as the second argument).</p>
<pre><code>{
  &quot;first list&quot;: [
    &quot;element1&quot;,
    &quot;element2&quot;,
    {
      &quot;element3&quot;: &quot;is another k-v pair&quot;,
      &quot;but contains&quot;: [
        &quot;a&quot;,
        &quot;list&quot;,
        &quot;of&quot;,
        &quot;words&quot;
      ]
    }
  ],
  &quot;this value is a string&quot;: &quot;1&quot;,
  &quot;and this is a number&quot;: 23,
  &quot;and floating point&quot;: 1.413
}</code></pre>
<p>You can also use jq in a shell script to obtain, for example the second element of the first list:</p>
<pre><code>$ jq &#39;.&quot;first list&quot;[1]&#39; example.json
&quot;element2&quot;</code></pre>
<p>So to get the value associated to a key you use the notation <code>.key</code> and to get the k-th element you use the notation <code>[k-1]</code>. To remove the quotes on the string you can use the <code>-r</code> flag which stands for raw output.</p>
<p><code>jq(1)</code> also gives you a few more functionalities that can be useful like getting the number of elements in a list with the length function.</p>
<pre><code>$ jq &#39;length&#39;  example.json
3
$ jq &#39;.&quot;first list&quot;[2].&quot;but contains&quot; | length&#39;
4</code></pre>
<p>Another useful feature is getting the list of keys from a key-value pair list which can be done with the function <code>keys</code></p>
<pre><code>$ jq &#39;.&quot;first list&quot;[2] | keys[]&#39; example.json
&quot;but contains&quot;,
&quot;element3&quot;</code></pre>
<p>The query language is much much more flexible than this, but for most cases this should be enough for simple configuration querying.</p>
<h1 id="yaml-and-xml">YAML and XML??</h1>
<p><a href="https://pypi.org/project/yq/">The yq project</a> allows one to use the exact same syntax as jq to query, and emit (and therefore also transcode) yaml and XML, extending the usefulness of the query language.</p>
<p>So for example looking at the previous file through <code>yq</code> gives:</p>
<pre><code>$ yq -y &#39;&#39; example.json
first list:
  - element1
  - element2
  - element3: is another k-v pair
    but contains:
      - a
      - list
      - of
      - words
this value is a string: &#39;1&#39;
and this is a number: 23
and floating point: 1.413</code></pre>
<p>And the output of this can be of course queried with <code>yq</code> itself, or can be used to feed into whatever application requires a yaml input (I guess it lacks the triple dash at the top, but that is actually the only warning I get from passing that abomination to yamllint)</p>
<p>Similarly <code>xq</code> can be used to query XML files with the same language. However, to emit these files from json you need to use <code>yq -x</code> like so:</p>
<pre><code>$ yq -x &#39;&#39; example2.json
&lt;file&gt;
  &lt;first_list&gt;element1&lt;/first_list&gt;
  &lt;first_list&gt;element2&lt;/first_list&gt;
  &lt;first_list&gt;
    &lt;element3&gt;is another k-v pair&lt;/element3&gt;
    &lt;but_contains&gt;a&lt;/but_contains&gt;
    &lt;but_contains&gt;list&lt;/but_contains&gt;
    &lt;but_contains&gt;of&lt;/but_contains&gt;
    &lt;but_contains&gt;words&lt;/but_contains&gt;
  &lt;/first_list&gt;
  &lt;this_value_is_a_string&gt;1&lt;/this_value_is_a_string&gt;
  &lt;and_this_is_a_number&gt;23&lt;/and_this_is_a_number&gt;
  &lt;and_floating_point&gt;1.413&lt;/and_floating_point&gt;
&lt;/file&gt;</code></pre>
<p>where the original (modified) file <code>example2.json</code> looks like:</p>
<pre><code>{
    &quot;file&quot;:
    {
      &quot;first_list&quot;: [
        &quot;element1&quot;,
        &quot;element2&quot;,
        {
          &quot;element3&quot;: &quot;is another k-v pair&quot;,
          &quot;but_contains&quot;: [
            &quot;a&quot;,
            &quot;list&quot;,
            &quot;of&quot;,
            &quot;words&quot;
          ]
        }
      ],
      &quot;this_value_is_a_string&quot;: &quot;1&quot;,
      &quot;and_this_is_a_number&quot;: 23,
      &quot;and_floating_point&quot;: 1.413
    }
}</code></pre>
<p>So that the root dictionary has a single key-value pair and all the keys have no spaces in them (so that they can be made into xml tags).</p>
