Home > Blockchain >  jq loop on items in new lines
jq loop on items in new lines

Time:11-13

I could read all of the entries it was set to, but they are put in one line instead of appending on new line.

Source:

#!/bin/bash

for row in $(jq -r '. | @base64' recipe.json); do
    _jq() {
     echo ${row} | base64 --decode | jq -r ${1}
    }
   #echo $(_jq '.dependencies[].name')
   echo $(_jq '.dependencies[].name') >> file.log
done

recipe.json:

{
    "florecipe-v": "1.0",
    "name": "web3updater",
    "dependencies": [{
            "name": "ethereal",
            "version": "2.6.0",
            "source": {
                "ipfs": "QmWmxakbb7msHFaD9QmGXBAmLpUHTdZG64BWhQg8dyx8gM",
                "github": "https://github.com/wealdtech/ethereal.git"
            },
            "linux-amd64": {
                "ipfs": "QmbwABiXz4U5MrmJLisfxaHEG7CyJKEnNECRjXogS7eAFw",
                "github": "https://github.com/wealdtech/ethereal/releases/download/v2.6.0/ethereal-2.6.0-linux-amd64.tar.gz"
            },
            "linux-arm64": {
                "ipfs": "QmV9agwSZuVXMJNYV1NhU8SY8TRg73z8RgTw4QaYrVxtrr",
                "github": "https://github.com/wealdtech/ethereal/releases/download/v2.6.0/ethereal-2.6.0-linux-arm64.tar.gz"
            },
            "windows-i386": {
                "ipfs": "",
                "github": ""
            }
        },
        {
            "name": "new",
            "version": "",
            "source": {
                "ipfs": "",
                "github": ""
            },
            "linux-amd64": {
                "ipfs": "",
                "github": ""
            },
            "linux-arm64": {
                "ipfs": "",
                "github": ""
            },
            "windows-i386": {
                "ipfs": "",
                "github": ""
            }
        }
    ]
}

file.log properly contains ethereal and new, but they are on the same line instead of appending.

CodePudding user response:

Instead of the dubious:

echo $(_jq '.dependencies[].name')

write:

_jq '.dependencies[].name'

IOW, KIS.


And speaking of KIS, have you considered using @base64d? As in:

printf "%s" "${row}" | jq -Rr "@base64d | fromjson | ${1}"
  • Related