title: Using jq to split a string into nested objects
body: <p>I'm trying to export data from consul kv into json with a nested object for each level of depth in consul.</p>
  
  <p>Example json returned from consul:</p>
  
  <pre><code>[
    {
      "LockIndex": 0,
      "Key": "foobar/testing",
      "Flags": 0,
      "Value": "xxxxx",
      "CreateIndex": 833,
      "ModifyIndex": 833
    },
    {
      "LockIndex": 0,
      "Key": "foobar/bazbar",
      "Flags": 0,
      "Value": "xxxxx",
      "CreateIndex": 833,
      "ModifyIndex": 833
    }
  ]
  </code></pre>
  
  <p>Desired JSON:</p>
  
  <pre><code>[
    {
      "foobar": {
        "testing": "xxxxx",
        "bazbar": "xxxxx"
      }
    }
  ]
  </code></pre>
  
  <p>I'm sort of close with <code>jq '.[] | objects | {Key: .Key | split("/"), Value: .Value}'</code> but I'm just not understanding how I can recurse based on a split() of .Key and create nested objects. I think I'll also need to sort_by(.Key) to handle out of order data, unless I can <code>| add</code> arbitrarily and have jq resolve the structure.</p>
  
  <p>I realize <code>xxxxx</code> is base64 encoded, and hopefully, <code>base64d</code> will get merged soon but until then I think I can handle decoding this with shell post-processing.</p>