How I can use shell variables on the jq command
For example
ID="THE_ID: .ID"
jq '. |{ $ID}' file.json
any ideas ?
See the --arg and --arg-file arguments.
Thanks Nico for your prompt reply ..
I have tried jq --arg ID "THE_ID: .ID"" file.son -- and it didn't work
and basically my aim is to use several variable in bash to be the query arguments of jq as $ID
What does "THE_ID: .ID" mean to you, exactly? The way --arg works is as
follows: --arg ARGNAME ARGVALUE. Then in your jq program you can say
"$ARGNAME" to reference the value.
Also, can we have a slightly more concrete example of what you're trying to
do?
On Mon, Nov 24, 2014 at 6:11 PM, kboghdady [email protected] wrote:
Thanks Nico for your prompt reply ..
I have tried jq --arg ID "THE_ID: .ID"" file.son -- and it didn't work
and basically my aim is to use several variable in bash to be the query
arguments of jq as $ID—
Reply to this email directly or view it on GitHub
https://github.com/stedolan/jq/issues/626#issuecomment-64282555.
I have tried jq --arg ID "THE_ID: .ID"" file.son -- and it didn't work
and basically my aim is to use several variable in bash to be the query
arguments of jq as $IDThe
--argvariable values are always taken to be strings. Which means
that{ $ID }is not a valid jq expression.
See also the FAQ - General Questions (https://github.com/stedolan/jq/wiki/FAQ#general-questions)
If you want whatever you pass as argument to be evaluated as a JSON object, you can pipe it to fromjson inside your jq script. For example, on a bash-like shell:
export FOO='{"a": 1, "b": 2}'
jq -n --arg foo "$FOO" '$foo | fromjson + {"c": 3}'
Thanks a lot for all the replies ..
I have fixed it by changing the ' to " so it could interrupt the value of $ID
jq ". [] | { $ID}" file.json
Also note that when you are using single quotes around the jq query, you should NOT double quote around jq --arg variables within the query, it confuses and breaks things. This "shell-ism" is a habit that tripped me up in jq.
This first example would work:
projectID=$(cat file.json | jq -r --arg EMAILID "$EMAILID" '.resource[]
| select(.username==$EMAILID) | .id')
This second example probably wouldn't:
projectID=$(cat file.json | jq -r --arg EMAILID "$EMAILID" '.resource[]
| select(.username=="$EMAILID") | .id')
jq '."media"."track"[] | select(."@type"=="General") | ."Encoded_Application"'
{
"media": {
"@ref": "2016-07-13_03-01-49.flv",
"track": [
{
"@type": "General",
"VideoCount": "1",
"AudioCount": "1",
"FileExtension": "flv",
"Format": "Flash Video",
"FileSize": "739869",
"Duration": "2.233",
"OverallBitRate": "2650673",
"FrameRate": "30.000",
"FrameCount": "67",
"File_Modified_Date": "UTC 2016-07-12 19:01:52",
"File_Modified_Date_Local": "2016-07-13 03:01:52",
"Encoded_Application": "Lavf56.40.101"
},
{
"@type": "Video",
"Format": "AVC",
"Format_Profile": "Main",
"Format_Level": "3.1",
"Format_Settings_CABAC": "Yes",
"Format_Settings_RefFrames": "4",
"CodecID": "7",
"Duration": "2.233",
"BitRate": "2500000",
"Width": "1280",
"Height": "720",
"Sampled_Width": "1280",
"Sampled_Height": "720",
"PixelAspectRatio": "1.000",
"DisplayAspectRatio": "1.778",
"FrameRate_Mode": "CFR",
"FrameRate": "30.000",
"FrameCount": "67",
"ColorSpace": "YUV",
"ChromaSubsampling": "4:2:0",
"BitDepth": "8",
"ScanType": "Progressive",
"Delay": "0.000",
"StreamSize": "697812",
"Encoded_Library": "x264 - core 142",
"Encoded_Library_Name": "x264",
"Encoded_Library_Version": "core 142",
"Encoded_Library_Settings": "cabac=1 / ref=1 / deblock=1:0:0 / analyse=0x1:0x111 / me=hex / subme=2 / psy=1 / psy_rd=1.00:0.00 / mixed_ref=0 / me_range=16 / chroma_me=1 / trellis=0 / 8x8dct=0 / cqm=0 / deadzone=21,11 / fast_pskip=1 / chroma_qp_offset=0 / threads=12 / lookahead_threads=4 / sliced_threads=0 / nr=0 / decimate=1 / interlaced=0 / bluray_compat=0 / constrained_intra=0 / bframes=3 / b_pyramid=2 / b_adapt=1 / b_bias=0 / direct=1 / weightb=1 / open_gop=0 / weightp=1 / keyint=120 / keyint_min=12 / scenecut=40 / intra_refresh=0 / rc_lookahead=10 / rc=cbr / mbtree=1 / bitrate=2500 / ratetol=1.0 / qcomp=0.60 / qpmin=0 / qpmax=69 / qpstep=4 / vbv_maxrate=2500 / vbv_bufsize=2500 / nal_hrd=none / filler=1 / ip_ratio=1.40 / aq=1:1.00"
},
{
"@type": "Audio",
"Format": "AAC",
"Format_AdditionalFeatures": "LC",
"CodecID": "10-2",
"Duration": "2.229",
"BitRate": "156250",
"Channels": "2",
"ChannelPositions": "Front: L R",
"ChannelLayout": "L R",
"SamplesPerFrame": "1024",
"SamplingRate": "44100",
"SamplingCount": "98299",
"FrameRate": "43.066",
"Compression_Mode": "Lossy",
"Delay": "0.000",
"Delay_Source": "Container",
"StreamSize": "43535",
"StreamSize_Proportion": "0.05884"
}
]
}
}
Most helpful comment
Also note that when you are using single quotes around the jq query, you should NOT double quote around jq --arg variables within the query, it confuses and breaks things. This "shell-ism" is a habit that tripped me up in jq.
This first example would work:
This second example probably wouldn't: