Jump to content

Construct script filter JSON with jq from bash array


Recommended Posts

Hi, I am creating a workflow and I want to create a script filter with strings from a bash array. Given I have an array called $regions that contains 3 strings. I want to create 3 list items from that using jq.

echo $regions
france netherlands denmark

I tried this command but the output is obviously not compliant with Alfreds JSON format.

jq -n --arg inarr "${regions}" '{ items: [ $inarr | split("\n") ] }'
{
  "items": [
    [
      "france",
      "netherlands",
      "denmark"
    ]
  ]
}

I need the JSON to look like that:

{"items": [
    {
        "uid": "france",
        "title": "france",
        "arg": "france",
        "autocomplete": "france",
        "icon": {
            "path": "icons/france.png"
        }
    },

    {
        "uid": "netherlands",
        "title": "netherlands",
        "arg": "netherlands",
        "autocomplete": "netherlands",
        "icon": {
            "path": "icons/netherlands.png"
        }
    },

    {
        "uid": "denmark",
        "title": "denmark",
        "arg": "denmark",
        "autocomplete": "denmark",
        "icon": {
            "path": "icons/denmark.png"
        }
    },
]}

Any help would be much appreciated.

Link to comment
  • 1 month later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...