title: Transform json to ini files using jq + bash
body: <p>I'm trying to transform json (array of objects) to ini files:</p>
  
  <pre><code>[
    {
      "connection": {
        "id": "br0",
        "uuid": "ab1dd903-4786-4c7e-a4b4-3339b144d6c7",
        "stable-id": "",
        "type": "bridge",
        "interface-name": "br0",
        "autoconnect": "no",
        "autoconnect-priority": "0",
        "autoconnect-retries": "-1",
        "auth-retries": "-1",
        "timestamp": "44444",
        "read-only": "no",
        "permissions": "",
        "zone": "WAN",
        "master": "",
        "slave-type": "",
        "autoconnect-slaves": "1",
        "secondaries": "",
        "gateway-ping-timeout": "0",
        "metered": "unknown",
        "lldp": "default"
      },
      "ipv4": {
        "method": "manual",
        "dns": "192.168.1.1,192.168.2.1",
        "dns-search": "",
        "dns-options": " ",
        "dns-priority": "0",
        "addresses": "192.168.1.3/24",
        "gateway": "",
        "routes": "192.168.10.0/24 192.168.1.1",
        "route-metric": "-1",
        "route-table": "0",
        "ignore-auto-routes": "no",
        "ignore-auto-dns": "no",
        "dhcp-client-id": "",
        "dhcp-timeout": "0",
        "dhcp-send-hostname": "yes",
        "dhcp-hostname": "",
        "dhcp-fqdn": "",
        "never-default": "no",
        "may-fail": "yes",
        "dad-timeout": "-1"
      }
    },
    {
      "connection": {
        ...
      },
    }
  ]
  </code></pre>
  
  <p>OR</p>
  
  <pre><code>  {
      "connection": {
        ...
      },
    }
  </code></pre>
  
  <p><strong>What i tried to do is:</strong></p>
  
  <p><strong>1. Transform json to strings</strong></p>
  
  <pre><code>data=$(jq -r 'def keyValue:  to_entries[] | "[\(.key)]\\n\(.value | to_entries|map("\(.key)=\(.value)" ) | join("\\n") )\\n"; if type == "array" then keys[] as $k | "\(.[$k] | .connection.id)=\(.[$k] | keyValue)" elif type == "object" then "\(.connection.id)=\(. | keyValue)" else keys end' /tmp/json)
  </code></pre>
  
  <p>Provides:</p>
  
  <pre><code>br1=[connection]\nid=br1\nuuid=ab1dd903-4786-4c7e-a4b4-3339b144d6c7\nstable-id=\ntype=fff\ninterface-name=br0\nautoconnect=no\nautoconnect-priority=0\nautoconnect-retries=-1\nauth-retries=-1\ntimestamp=1525494904\nread-only=no\npermissions=\nzone=WAN\nmaster=\nslave-type=\nautoconnect-slaves=1\nsecondaries=\ngateway-ping-timeout=0\nmetered=unknown\nlldp=default\n
  br1=[802-3-ethernet]\nport=\nspeed=0\nduplex=\nauto-negotiate=no\nmac-address=\ncloned-mac-address=\ngenerate-mac-address-mask=\nmac-address-blacklist=\nmtu=1500\ns390-subchannels=\ns390-nettype=\ns390-options=\nwake-on-lan=default\nwake-on-lan-password=\n....
  </code></pre>
  
  <p><strong>2. Walk over strings in bash</strong></p>
  
  <pre><code>while IFS="=" read -r key value; do [ "$oldkey" = "$key" ] &amp;&amp; echo -en "$value" &gt;&gt; "/tmp/ini/$key" || echo -en "$value" &gt; "/tmp/ini/$key" ; oldkey="$key"; done &lt;&lt;&lt; "$data"
  </code></pre>
  
  <p>Gives:</p>
  
  <pre><code>[connection]
  id=br1
  uuid=ab1dd903-4786-4c7e-a4b4-3339b144d6c7
  stable-id=
  type=fff
  interface-name=br0
  autoconnect=no
  autoconnect-priority=0
  autoconnect-retries=-1
  auth-retries=-1
  timestamp=1525494904
  read-only=no
  permissions=
  zone=WAN
  master=
  slave-type=
  autoconnect-slaves=1
  secondaries=
  gateway-ping-timeout=0
  metered=unknown
  lldp=default
  [ipv4]
  method=manual
  dns=192.168.1.1,192.168.2.1
  dns-search=
  dns-options= 
  dns-priority=0
  addresses=192.168.1.3/24
  gateway=
  routes=192.168.10.0/24 192.168.1.1
  route-metric=-1
  route-table=0
  ignore-auto-routes=no
  ignore-auto-dns=no
  dhcp-client-id=
  dhcp-timeout=0
  dhcp-send-hostname=yes
  dhcp-hostname=
  dhcp-fqdn=
  never-default=no
  may-fail=yes
  dad-timeout=-1
  </code></pre>
  
  <p>I'm almost there! But is there possible to do it more "elegantly" and more performance way, avoiding pipes, external calls, etc.</p>
  
  <p><strong>Note:</strong> Moreover, mostly it should be done with jq + bash, because other processing tools like sed, awk is slower than i've done, but i do not reject them completely =)</p>
  
  <p><strong>P.S.</strong> - Main purpose of this transforming is the fast "bulk operation" to write ini files</p>