Get value from array in json blob

Viewed 49

I am trying to get a value in second line of msg_values after colon:

{
    "txt": {
        "new": false,
        "msg": "Generating 'value'\nGenerated new value: +somestring12345=\n",
        "msg_values": [
            "Generating 'value'",
            "Generated new value: +somestring12345=",
            ""
        ]
    }
}

jq -r '.[].msg_values[]' test.json returns:

Generating 'value'
Generated new value: +somestring12345=

What I want is just a: +somestring12345=

I've tried with select and map but with no luck.

2 Answers

You could use index, e.g. along the lines of:

.txt|.msg_values[1]| index(":") as $ix | .[1+$ix:]

or more defensively:

.txt|.msg_values[1]| (index(":") // empty) as $ix | .[1+$ix:]

Since it’s not clear how exactly you want to trim the value, I’ll leave that detail to you.

Is using sed an option here?

jq -r '.[].msg_values[1]' test.json | sed -r "s/(.*):(.*)/\2/"
Related