0

I've been experimenting for quite a while now and also researched stackoverflow and came across this question which comes closes to what I'm trying to achieve: How to combine an array into a single string value when using CSV output in jq?

My JSON output looks quite a big more complicated though:

"content": [
{
    "type": "paragraph",
    "content": [
        {
            "type": "inlineCard",
            "attrs": {
            "url": "https://www.myurl.com"
            }
        },
        {
            "type": "text",
            "text": " "
        }
    ]
},
{
    "type": "paragraph",
    "content": [
        {
            "type": "inlineCard",
            "attrs": {
                "url": "https://www.anotherurl.com"
            }
        },
        {
            "type": "text",
            "text": " "
        }
    ]
},
{
    "type": "paragraph",
    "content": [
        {
            "type": "inlineCard",
            "attrs": {
                "url": "https://www.andanotherurl.com"
            }
        },
        {
            "type": "text",
            "text": " "
        }
    ]
}
]

I would now like to get the following result:

"https://www.myurl.com, https://www.anotherurl.com, https://www.andanotherurl.com"

I've tried the following:

echo $MY_JSON | jq '[ .content[].content[] | select(.type | contains("inlineCard")) | .attrs.url | join(",") ]'

but that tells me

jq: error (at <stdin>:0): Cannot iterate over string ("http://ww...)

Can anyone help me? Thank you!

Peter Meier
  • 483
  • 3
  • 7
  • 17

1 Answers1

2

I just found the solution myself, the trick was to end the array before sending it to the join function:

echo $MY_JSON | jq '[ .content[].content[] | select(.type | contains("inlineCard")) | .attrs.url ] | join(",")'
Peter Meier
  • 483
  • 3
  • 7
  • 17