1

with jq, I'd like to get "name" value of blocks where "Media" attribute (under .session.attributeList) contains "node7000" string (in example json first array block matches it - there are two matching:

"value": "node7000"

"value": "node7000 and node8000"

and expected jq output would be:

"Station 17200"

[{
    "name": "Station 17200",
    "attributes": [{
            "name": "EnableLog",
            "value": "1"
        }, {
            "name": "LogFont",
            "value": "0"
        }, {
            "name": "IdleTimer",
            "value": "30"
        }
    ],
    "session": [{
            "attributeList": [{
                    "name": "Launch",
                    "value": "1"
                }, {
                    "name": "Media",
                    "value": "node7000"
                }
            ]
        }, {
            "attributeList": [{
                    "name": "Group",
                    "value": "1"
                }, {
                    "name": "RMedia",
                    "value": "1"
                }
            ]
        }, {
            "attributeList": [{
                    "name": "Launch",
                    "value": ""
                }, {
                    "name": "Media",
                    "value": "node7000 and node8000"
                }
            ]
        }
    ]
},
{
    "name": "Station 17300",
    "attributes": [{
            "name": "EnableLog",
            "value": "1"
        }, {
            "name": "LogFont",
            "value": "0"
        }, {
            "name": "IdleTimer",
            "value": "30"
        }
    ],
    "session": [{
            "attributeList": [{
                    "name": "Launch",
                    "value": "1"
                }, {
                    "name": "Media",
                    "value": "node6000"
                }
            ]
        }, {
            "attributeList": [{
                    "name": "Group",
                    "value": "1"
                }, {
                    "name": "RMedia",
                    "value": "1"
                }
            ]
        }, {
            "attributeList": [{
                    "name": "Launch",
                    "value": ""
                }, {
                    "name": "Media",
                    "value": "node6001"
                }
            ]
        }
    ]
}]
2

jq solution:

jq '.[] | select([.session[].attributeList[] 
                   | .name == "Media" and (.value | contains("node7000"))
                 ] | any ).name' jsonfile
  • .[] - iterating over all elements(objects) of the input array
  • select(<condition>) - the function produces its input unchanged if <condition> returns true for that input, and produces no output otherwise
  • contains(element) - the filter produces true if element is completely contained within the input
  • any - the filter takes as input an array of boolean values and produces true as output if any of the elements of the array are true

The output:

"Station 17200"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.