Thursday, May 19, 2022

[FIXED] How to do double-indirection with jq variables?

Issue

How to construct a name of a variable from data and access that variable?

For example it should somehow give the content of the alpha-file:

jq '$.var"-file"' --slurpfile alpha-file <(echo 0) --slurpfile beta-file <(echo 1) <<<'{"var": "alpha"}'

It should output:

[
  0
]

Solution

Named arguments are also available to the jq program as $ARGS.named.

So there is a dictionary to extract variables by a string name from:

jq '$ARGS.named["\(.var)-file"]' --slurpfile alpha-file <(echo 0) --slurpfile beta-file <(echo 1) <<<'{"var": "alpha"}'

outputs

[
  0
]


Answered By - Velkan
Answer Checked By - Timothy Miller (PHPFixing Admin)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.