Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
456 views
in Technique[技术] by (71.8m points)

json - passing arguments to jq filter

Here is my config.json:

{
    "env": "dev",
    "dev": {
        "projects" : {
            "prj1": {
                "dependencies": {},
                "description": ""
            }
        }
    }
}

Here are my bash commands:

PRJNAME='prj1'

echo $PRJNAME

jq --arg v "$PRJNAME" '.dev.projects."$v"' config.json 
jq '.dev.projects.prj1' config.json 

The output:

prj1
null
{
  "dependencies": {},
  "description": ""
}

So $PRJNAME is prj1, but the first invocation only outputs null.

Can someone help me?

question from:https://stackoverflow.com/questions/34745451/passing-arguments-to-jq-filter

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The jq program .dev.projects."$v" in your example will literally try to find a key named "$v". Try the following instead:

jq --arg v "$PRJNAME" '.dev.projects[$v]' config.json 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...