Michel Jorda
Michel Jorda

Reputation: 27

Use value as key for another object in jq

I setup a json config file like this

{
    "current": "config1",
    "config1": {
        "p1": "val1-1",
        "p2": "val1-2"
    },
    "config2": {
        "p1": "val2-1",
        "p2": "val2-2"
    }
}

I would like to get "current" config with bash and jq. At the moment I use:

#!/usr/bin/env bash

C=`jq '.current' $1`
D=`eval jq '."$C"' $1`
echo " ------  D is $D"
echo $D | jq  '.p1'

Is there a direct jq filter/key trick to obtain the same result?

Upvotes: 2

Views: 265

Answers (1)

Inian
Inian

Reputation: 85865

You just need to use the .[] object value iterator syntax to first let .current expand to its constituent value and then the expression becomes ."config1" which matches the config1 key

jq '.[.current]'

Upvotes: 4

Related Questions