Query Json data saved in Postgres - json

I'm trying to query JSON data saved in postgres.
This is how the table is created
CREATE TABLE ALARMDATA2(ALARM CHAR(1300))
This is the JSON object:
{"delay_max": 0.0, "ts_errors": [{"count": 0, "state": 0, "is_priority1": true, "name": "SYNC", "is_priority2": false}, {"count": 0, "state": 0, "is_priority1": true, "name": "BYTE", "is_priority2": false}, {"count": 0, "state": 0, "is_priority1": true, "name": "PAT", "is_priority2": false}, {"count": 0, "state": 0, "is_priority1": true, "name": "CC", "is_priority2": false}, {"count": 0, "state": 0, "is_priority1": true, "name": "PMT", "is_priority2": false}, {"count": 0, "state": 0, "is_priority1": true, "name": "PID", "is_priority2": false}, {"count": 0, "state": 0, "is_priority1": false, "name": "TS", "is_priority2": true}, {"count": 0, "state": 0, "is_priority1": false, "name": "CRC", "is_priority2": true}, {"count": 0, "state": 0, "is_priority1": false, "name": "PCR", "is_priority2": true}, {"count": 0, "state": 0, "is_priority1": false, "name": "ACC", "is_priority2": true}, {"count": 0, "state": 0, "is_priority1": false, "name": "PTS", "is_priority2": true}, {"count": 0, "state": 0, "is_priority1": false, "name": "CAT", "is_priority2": true}], "is_stream_paused": false, "delay_min": 0.0, "ac_err": 0.0, "oj_err": 0.0}
I'd like to query based on "delay_max" (the first entry in JSON). I am using this query
SELECT ALARM->>'delay_max' AS delay_max FROM alarmdata2;
I think the query syntax is fine as per these links (here and here) but I am getting this error
HINT: No operator matches the given name and argument type(s). You
might need to add explicit type casts.
I've been searching for a while but I have not clue.
Any suggestions why ?

You must define the ALARM colum as type JSON or JSONB:
CREATE TABLE ALARMDATA2(ALARM JSONB)
And then it will work.

Fix the problem
ALTER TABLE alarmdata2
ALTER COLUMN alarm
TYPE jsonb
USING alarm::jsonb;
Or, work around it
SELECT ALARM::jsonb->>'delay_max' AS delay_max
FROM alarmdata2;

Related

Looping a nesty struture in Coldfusion [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last month.
Improve this question
Here is my JSON Data:
This i am trying to loop and get all the information about every node of it with the key name and the key data
{
"src": 1,
"feeds": [{
"id": 1,
"name": "All",
"polarPlot": true
}],
"srcFeed": 1,
"showSil": true,
"showFlg": true,
"showPic": true,
"flgH": 20,
"flgW": 85,
"acList": [{
"Id": 12589424,
"Rcvr": 1,
"HasSig": false,
"Icao": "C01970",
"Bad": false,
"Reg": "C-FJQL",
"FSeen": "\/Date(1672216397120)\/",
"TSecs": 334,
"CMsgs": 330,
"Alt": 34975,
"GAlt": 35206,
"InHg": 30.15059,
"AltT": 0,
"Call": "ROU1958",
"Lat": 45.59019,
"Long": -74.90746,
"PosTime": 1672216731130,
"Mlat": false,
"Tisb": false,
"Spd": 469.0,
"Trak": 59.3,
"TrkH": false,
"Type": "A321",
"Mdl": "Airbus A321 211SL",
"Man": "Airbus",
"CNum": "7117",
"From": "YYZ Lester B. Pearson, Toronto, Canada",
"To": "YQB Quebec Jean Lesage, Canada",
"Op": "Air Canada Rouge",
"OpIcao": "ROU",
"Sqk": "2222",
"Help": false,
"Vsi": 0,
"VsiT": 0,
"WTC": 2,
"Species": 1,
"Engines": "2",
"EngType": 3,
"EngMount": 0,
"Mil": false,
"Cou": "Canada",
"HasPic": false,
"Interested": false,
"FlightsCount": 2,
"Gnd": false,
"SpdTyp": 0,
"CallSus": false,
"Trt": 2,
"Year": "2016"
}, {
"Id": 12595324,
"Rcvr": 1,
"HasSig": false,
"Icao": "C0307C",
"Bad": false,
"Reg": "C-FSJJ",
"FSeen": "\/Date(1672215986531)\/",
"TSecs": 745,
"CMsgs": 284,
"Alt": 35000,
"GAlt": 35189,
"InHg": 30.1092529,
"AltT": 0,
"Call": "ACA394",
"Lat": 47.86931,
"Long": -72.90211,
"PosTime": 1672216731064,
"Mlat": false,
"Tisb": false,
"Spd": 513.0,
"Trak": 117.2,
"TrkH": false,
"Type": "B38M",
"Mdl": "Boeing 737MAX 8",
"Man": "Boeing",
"CNum": "61217",
"From": "YYC Calgary, Canada",
"To": "YHZ Halifax / Stanfield, Canada",
"Op": "Air Canada",
"OpIcao": "ACA",
"Sqk": "1144",
"Help": false,
"Vsi": 0,
"VsiT": 0,
"WTC": 2,
"Species": 1,
"Engines": "2",
"EngType": 3,
"EngMount": 0,
"Mil": false,
"Cou": "Canada",
"HasPic": false,
"Interested": false,
"FlightsCount": 2,
"Gnd": false,
"SpdTyp": 0,
"CallSus": false,
"Trt": 2,
"Year": "2018"
}, {
"Id": 12601386,
"Rcvr": 1,
"HasSig": false,
"Icao": "C0482A",
"Bad": false,
"Reg": "C-GBIN",
"FSeen": "\/Date(1672215747131)\/",
"TSecs": 984,
"CMsgs": 5124,
"Alt": 32925,
"GAlt": 33185,
"InHg": 30.1801186,
"AltT": 0,
"Call": "ROU1689",
"Lat": 45.119546,
"Long": -71.834575,
"PosTime": 1672216731191,
"Mlat": false,
"Tisb": false,
"Spd": 462.5,
"Trak": 31.1,
"TrkH": false,
"Type": "A319",
"Mdl": "Airbus A319 114",
"Man": "Airbus",
"CNum": "845",
"From": "FLL Fort Lauderdale Hollywood, United States",
"To": "YQB Quebec Jean Lesage, Canada",
"Op": "Air Canada Rouge",
"OpIcao": "ROU",
"Sqk": "1447",
"Help": false,
"Vsi": -2112,
"VsiT": 0,
"WTC": 2,
"Species": 1,
"Engines": "2",
"EngType": 3,
"EngMount": 0,
"Mil": false,
"Cou": "Canada",
"HasPic": false,
"Interested": false,
"FlightsCount": 1,
"Gnd": false,
"SpdTyp": 0,
"CallSus": false,
"Trt": 2,
"Year": "1998"
}, {
"Id": 10812570,
"Rcvr": 1,
"HasSig": false,
"Icao": "A4FC9A",
"Bad": false,
"Reg": "N420LA",
"FSeen": "\/Date(1672215081608)\/",
"TSecs": 1650,
"CMsgs": 4226,
"Alt": 34000,
"GAlt": 34281,
"InHg": 30.2007866,
"AltT": 0,
"Call": "LAE2512",
"Lat": 46.00371,
"Long": -71.20912,
"PosTime": 1672216731894,
"Mlat": false,
"Tisb": false,
"Spd": 399.0,
"Trak": 210.7,
"TrkH": false,
"Type": "B763",
"Mdl": "Boeing 767 316F/W",
"Man": "Boeing",
"CNum": "34627",
"Op": "MAS Air Cargo",
"OpIcao": "MAA",
"Sqk": "0706",
"Help": false,
"Vsi": 0,
"VsiT": 0,
"WTC": 3,
"Species": 1,
"Engines": "2",
"EngType": 3,
"EngMount": 0,
"Mil": false,
"Cou": "United States",
"HasPic": false,
"Interested": false,
"FlightsCount": 1,
"Gnd": false,
"SpdTyp": 0,
"CallSus": false,
"Trt": 2,
"Year": "2006"
}, {
"Id": 12706028,
"Rcvr": 1,
"HasSig": false,
"Icao": "C1E0EC",
"Bad": false,
"FSeen": "\/Date(1672196473179)\/",
"TSecs": 20258,
"CMsgs": 1605,
"Alt": 125300,
"GAlt": 125300,
"AltT": 0,
"Call": "TEST1234",
"Tisb": false,
"TrkH": false,
"Sqk": "",
"VsiT": 0,
"WTC": 0,
"Species": 0,
"EngType": 0,
"EngMount": 0,
"Mil": false,
"Cou": "Canada",
"HasPic": false,
"Interested": false,
"FlightsCount": 4,
"Gnd": false,
"SpdTyp": 0,
"CallSus": false,
"Trt": 1
}],
"totalAc": 5,
"lastDv": "638077811342841157",
"shtTrlSec": 30,
"stm": 1672216731909
}
I am trying to loop and have the data for every loop in the nesty structure
As depending on the plane info, data is often changed fopr more or less inside nesty loop
I have tried everything on my low knowledge of coldfusion
Need some help and many thanks to all
Save it as a variable, then deserialize.
<cfset JSONObject = DeserializeJSON(YourVariable)>
You can then call specific key-values from it like #JSONObject.flgH#.
If you have multiple to loop through you can output it with array notation, like #JSONObject.acList.Reg[1]# - you would replace the number with a variable like [i] if you're looping through it dynamically.
You can start with:
<cfset local.stData = deserializeJSON(whatever that variable is holding the JSON) />
<!--- Now, local.stData.acList is what is called "an array of structs" --->
<cfloop array="#local.stData.acList#" index="local.n" item="local.stPlane">
<!--- In here, you can work with local.stPlane.id, for example --->
<!--- If you need to loop over the "keys" of the struct, like "Id", "Rcvr", you can do that, too. I prefer structKeyList --->
<cfloop list="#structKeyList(local.stPlane)#" index="local.sKey">
<cfif local.sKey = 'Id'>
<cfelseif local.sKey = 'Rcvr'>
</cfif>
<!--- Or... --->
<cfswitch expression="#local.sKey#">
<cfcase value="Id">
</cfcase>
<cfcase value="Rcvr">
</cfcase>
<cfdefaultcase>
<!--- For unexpected keys, or just skip this block --->
</cfdefaultcase>
</cfswitch>
</cfloop>
</cfloop>

json jq request format

I need help to extract data from this JSON file using jq.
The app flv then verify the streamname mystrame is active and extract meta.video.height
I did try lot of queries without success and my jq knowledge is poor.
{
"port": 1935,
"server_index": 0,
"applications": [{
"name": "hls",
"live": {
"streams": [{
"name": "donbosco",
"time": 2380739,
"bw_in": 2112440,
"bytes_in": 541618713,
"bw_out": 0,
"bytes_out": 0,
"bw_audio": 35544,
"bw_video": 2076888,
"clients": [{
"id": 453,
"address": "127.0.0.1",
"time": 2380959,
"flashver": "FMLE/3.0 (compatible; Lavf57.83.100)",
"dropped": 0,
"avsync": 28,
"timestamp": 2382635,
"publishing": true,
"active": true
}],
"records": [],
"meta": {
"video": {
"width": 1168,
"height": 720,
"frame_rate": 25,
"codec": "H264",
"profile": "High",
"level": 3.1
},
"audio": {
"codec": "AAC",
"profile": "LC",
"channels": 2,
"sample_rate": 16000
}
},
"nclients": 1,
"publishing": true,
"active": true
}],
"nclients": 1
},
"recorders": {
"count": 0,
"lists": []
}
},
{
"name": "flv",
"live": {
"streams": [{
"name": "mystream",
"time": 2382811,
"bw_in": 2059096,
"bytes_in": 541841549,
"bw_out": 2059096,
"bytes_out": 543351459,
"bw_audio": 35472,
"bw_video": 2023624,
"clients": [{
"id": 452,
"address": "127.0.0.1",
"time": 2382727,
"flashver": "LNX 9,0,124,2",
"dropped": 0,
"avsync": -12,
"timestamp": 2384520,
"publishing": false,
"active": true
},
{
"id": 451,
"address": "127.0.0.1",
"time": 2383031,
"flashver": "FMLE/3.0 (compatible; Lavf58.74.100)",
"dropped": 0,
"avsync": -12,
"timestamp": 2384520,
"publishing": true,
"active": true
}
],
"records": [],
"meta": {
"video": {
"width": 1168,
"height": 720,
"frame_rate": 25,
"codec": "H264",
"profile": "High",
"level": 3.1
},
"audio": {
"codec": "AAC",
"profile": "LC",
"channels": 2,
"sample_rate": 16000
}
},
"nclients": 2,
"publishing": true,
"active": true
}],
"nclients": 2
},
"recorders": {
"count": 0,
"lists": []
}
}
]
}
Are you asking for help with the command-line JSON processor jq or the JavaScript library jQuery? They are not the same. For jq, try
jq '
.applications
| map(select(.name == "flv"))[].live.streams
| map(select(.name == "mystream" and .active))[].meta.video.height
'
720
Demo

Python 3 - Extracting value from key in nested dictionary

Hoping for some pointers here as I'm striking out with my attempts. Am working with python 3.8.5.
I'm querying a Car Park booking system that returns a json list of availability. The result has lots of nested dictionaries and I'm struggling to extract just the values i want.
This is what I've been doing:
import requests
import json
enquiry = requests.post(url.....) #queries api, this works fine
results = enquiry.text #extracts response data
dictionary = json.loads(results) #convert response to python dict
If there is one slot available, I get this output (apologies for length). If there are multiple slots available, i get the same output repeated:
{
"data": {
"services": [{
"id": null,
"name": null,
"services": [{
"id": null,
"name": "Car Park",
"met": true,
"filterCount": 1,
"primary": true,
"options": [{
"id": "9",
"images": [],
"available": true,
"calendarId": "AAAA",
"templateId": "BBBB",
"capacity": 0,
"name": "Car Park Slot 3",
"sessionId": "CCCC",
"functions": null,
"startDate": "2020-08-18T13:30:00Z", <--this is what I want to extract
"endDate": "2020-08-18T14:30:00Z",
"geo": {
"lat": 0.0,
"lng": 0.0
},
"selected": false,
"linkedServices": [],
"tiers": null
}]
}],
"currentBookingId": null,
"startDate": {
"ms": 1597757400000,
"year": 2020,
"month": 8,
"day": 18,
"dayOfWeek": 2,
"time": {
"seconds": 0,
"minutes": 30,
"hours": 14,
"days": 0
}
},
"endDate": {
"ms": 1597761000000,
"year": 2020,
"month": 8,
"day": 18,
"dayOfWeek": 2,
"time": {
"seconds": 0,
"minutes": 30,
"hours": 15,
"days": 0
}
},
"sessionId": "2222222",
"chargeType": 1,
"hasPrimaryBookable": false,
"hasBookable": false,
"hasDiscounts": false,
"hasMultipleTiers": false,
"isPreferred": false,
"primaryServiceAvailable": true,
"primaryServiceId": null,
"primaryServiceType": "undefined",
"unavailableAttendees": []
}],
"bookingLimit": null
},
"success": true,
"suppress": false,
"version": "2.3.293",
"message": null,
"result": null,
"errors": null,
"code": null,
"flags": 0,
"redirect": null
}
I want to extract:
"startDate": "2020-08-18T13:30:00Z"
from each key, value pair in any of the returned slots. However, I cant work it out.
Extracting the whole of this nested dictionary would also contain the same data, but would then involve more work to tidy it up after.
"startDate": {
"ms": 1597757400000,
"year": 2020,
"month": 8,
"day": 18,
"dayOfWeek": 2,
"time": {
"seconds": 0,
"minutes": 30,
"hours": 14,
"days": 0
}
I've tried loads of dictionary.get and dictionary.items variations, but cant seem to get anywhere.
I tried something like
key = ('startDate')
availability = dictionary.get(key)
print(availability)
this just returns 'none', so think im way off
Any pointers?
Thanks in advance!
Thanks for the full data. It makes testing easier :)
I had to replace null -> None, true -> True, false -> False
slot = {
"data": {
"services": [{
"id": None,
"name": None,
"services": [{
"id": None,
"name": "Car Park",
"met": True,
"filterCount": 1,
"primary": True,
"options": [{
"id": "9",
"images": [],
"available": True,
"calendarId": "AAAA",
"templateId": "BBBB",
"capacity": 0,
"name": "Car Park Slot 3",
"sessionId": "CCCC",
"functions": None,
"startDate": "2020-08-18T13:30:00Z", # <--this is what I want to extract
................
print("Start Date:", slot['data']['services'][0]['services'][0]['options'][0]['startDate'])
Output
Start Date: 2020-08-18T13:30:00Z

Get the codec_name of a specific stream

I need to extract the codec_name of the second video stream (artwork) of a video file.
So I do this :
$ ffprobe -hide_banner -v error -show_streams -of json Resurrection_Sunday_Online_Experience_12_April_2020_5.30pm_New_Creation_Church.mp4 | jq . > ffmpeg_output.json
The json file look like this :
$ cat ffmpeg_output.json
{
"streams": [
{
"index": 0,
"codec_name": "h264",
"codec_long_name": "H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10",
"profile": "Main",
"codec_type": "video",
"codec_time_base": "1411877/84712500",
"codec_tag_string": "avc1",
"codec_tag": "0x31637661",
"width": 854,
"height": 480,
"coded_width": 864,
"coded_height": 480,
"has_b_frames": 0,
"sample_aspect_ratio": "1:1",
"display_aspect_ratio": "427:240",
"pix_fmt": "yuv420p",
"level": 31,
"color_range": "tv",
"color_space": "bt709",
"color_transfer": "bt709",
"color_primaries": "bt709",
"chroma_location": "left",
"refs": 1,
"is_avc": "true",
"nal_length_size": "4",
"r_frame_rate": "30/1",
"avg_frame_rate": "42356250/1411877",
"time_base": "1/90000",
"start_pts": 0,
"start_time": "0.000000",
"duration_ts": 677700960,
"duration": "7530.010667",
"bit_rate": "387395",
"bits_per_raw_sample": "8",
"nb_frames": "225900",
"disposition": {
"default": 1,
"dub": 0,
"original": 0,
"comment": 0,
"lyrics": 0,
"karaoke": 0,
"forced": 0,
"hearing_impaired": 0,
"visual_impaired": 0,
"clean_effects": 0,
"attached_pic": 0,
"timed_thumbnails": 0
},
"tags": {
"language": "und",
"handler_name": "VideoHandler"
}
},
{
"index": 1,
"codec_name": "aac",
"codec_long_name": "AAC (Advanced Audio Coding)",
"profile": "-1",
"codec_type": "audio",
"codec_time_base": "1/48000",
"codec_tag_string": "mp4a",
"codec_tag": "0x6134706d",
"sample_fmt": "fltp",
"sample_rate": "48000",
"channels": 2,
"channel_layout": "stereo",
"bits_per_sample": 0,
"r_frame_rate": "0/0",
"avg_frame_rate": "0/0",
"time_base": "1/48000",
"start_pts": 0,
"start_time": "0.000000",
"duration_ts": 361441280,
"duration": "7530.026667",
"bit_rate": "125374",
"max_bit_rate": "129296",
"nb_frames": "352970",
"disposition": {
"default": 1,
"dub": 0,
"original": 0,
"comment": 0,
"lyrics": 0,
"karaoke": 0,
"forced": 0,
"hearing_impaired": 0,
"visual_impaired": 0,
"clean_effects": 0,
"attached_pic": 0,
"timed_thumbnails": 0
},
"tags": {
"language": "und",
"handler_name": "SoundHandler"
}
},
{
"index": 2,
"codec_name": "mjpeg",
"codec_long_name": "Motion JPEG",
"profile": "192",
"codec_type": "video",
"codec_time_base": "0/1",
"codec_tag_string": "[0][0][0][0]",
"codec_tag": "0x0000",
"width": 1280,
"height": 720,
"coded_width": 1280,
"coded_height": 720,
"has_b_frames": 0,
"sample_aspect_ratio": "1:1",
"display_aspect_ratio": "16:9",
"pix_fmt": "yuvj420p",
"level": -99,
"color_range": "pc",
"color_space": "bt470bg",
"chroma_location": "center",
"refs": 1,
"r_frame_rate": "90000/1",
"avg_frame_rate": "0/0",
"time_base": "1/90000",
"start_pts": 0,
"start_time": "0.000000",
"duration_ts": 677702430,
"duration": "7530.027000",
"bits_per_raw_sample": "8",
"disposition": {
"default": 0,
"dub": 0,
"original": 0,
"comment": 0,
"lyrics": 0,
"karaoke": 0,
"forced": 0,
"hearing_impaired": 0,
"visual_impaired": 0,
"clean_effects": 0,
"attached_pic": 1,
"timed_thumbnails": 0
}
}
]
}
I tried this but it does not extract the codec_name properly
$ cat ffmpeg_output.json | jq '[select(.streams[].codec_type=="video")][1].codec_name'
null
I also noticed it selected all the streams :
$ cat ffmpeg_output.json | jq '[select(.streams[].codec_type=="video")][1]' | grep codec_type
"codec_type": "video",
"codec_type": "audio",
"codec_type": "video",
Can you help me ?
Your jq filter is simply incorrect. Your select() expression is wrong. Remember the select() expression returns a Boolean value and your condition .streams[].codec_type=="video" is asserted true for two of the provided input objects i.e. for streams with "index": 0 and "index": 2, and for each of these true conditions, the filter prints all the 3 objects i.e. replicates the whole JSON because the select() was at the top level.
You need to make the select() after .streams
.streams[] | select(.codec_type=="video").codec_name
For selecting a object at a specific index, use the standard array notation .[0], .[1]
[ .streams[] | select(.codec_type=="video") ][1] | .codec_name
You don't need JQ for this particular case.
$ ffprobe -hide_banner -v error -of compact=nk=1:p=0 -select_streams v:1 -show_entries stream=codec_name video.mp4
h264

Create a string from multiple tags in json file in jq

I am trying to get multiple values from keys inside json. I have managed the first part, but the second part is not giving me what I want.
Here is a snippet of some data in json of a video:
{
"streams": [
{
"index": 0,
"codec_name": "h264",
"codec_long_name": "H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10",
"profile": "Main",
"codec_type": "video",
"codec_time_base": "0/2",
"codec_tag_string": "[27][0][0][0]",
"codec_tag": "0x001b",
"width": 1920,
"height": 1080,
"coded_width": 1920,
"coded_height": 1088,
"has_b_frames": 1,
"sample_aspect_ratio": "1:1",
"display_aspect_ratio": "16:9",
"pix_fmt": "yuv420p",
"level": 40,
"chroma_location": "left",
"field_order": "progressive",
"refs": 1,
"is_avc": "false",
"nal_length_size": "0",
"id": "0x100",
"r_frame_rate": "25/1",
"avg_frame_rate": "0/0",
"time_base": "1/90000",
"start_pts": 1397779146,
"start_time": "15530.879400",
"bits_per_raw_sample": "8",
"disposition": {
"default": 0,
"dub": 0,
"original": 0,
"comment": 0,
"lyrics": 0,
"karaoke": 0,
"forced": 0,
"hearing_impaired": 0,
"visual_impaired": 0,
"clean_effects": 0,
"attached_pic": 0,
"timed_thumbnails": 0
}
},
{
"index": 1,
"codec_name": "aac",
"codec_long_name": "AAC (Advanced Audio Coding)",
"codec_type": "audio",
"codec_time_base": "1/0",
"codec_tag_string": "[15][0][0][0]",
"codec_tag": "0x000f",
"sample_fmt": "fltp",
"sample_rate": "0",
"channels": 0,
"bits_per_sample": 0,
"id": "0x101",
"r_frame_rate": "0/0",
"avg_frame_rate": "0/0",
"time_base": "1/90000",
"disposition": {
"default": 0,
"dub": 0,
"original": 0,
"comment": 0,
"lyrics": 0,
"karaoke": 0,
"forced": 0,
"hearing_impaired": 0,
"visual_impaired": 0,
"clean_effects": 0,
"attached_pic": 0,
"timed_thumbnails": 0
},
"tags": {
"language": "eng"
}
},
{
"index": 2,
"codec_name": "dvb_teletext",
"codec_long_name": "DVB teletext",
"codec_type": "subtitle",
"codec_tag_string": "[6][0][0][0]",
"codec_tag": "0x0006",
"id": "0x102",
"r_frame_rate": "0/0",
"avg_frame_rate": "0/0",
"time_base": "1/90000",
"disposition": {
"default": 0,
"dub": 0,
"original": 0,
"comment": 0,
"lyrics": 0,
"karaoke": 0,
"forced": 0,
"hearing_impaired": 0,
"visual_impaired": 0,
"clean_effects": 0,
"attached_pic": 0,
"timed_thumbnails": 0
},
"tags": {
"language": "eng"
}
}
]
}
So I want to get the video information first followed by audio information:
Expected result:
"1080,h264,aac"
Here is the code I am trying, which gives me a blank output:
.streams[] | "\(select(.codec_type=="video") | "\(.height),\(.codec_name)"),\(select(.codec_type=="audio") | "\(.codec_name)")"
However, when referencing a single array, it works:
.streams[] | "\(select(.codec_type=="video") | "\(.height),\(.codec_name)"),"
"1080,h264,"
I want both audio and video information.
Jq Play
The following seems to be what you want:
.streams
| (.[] | select(.codec_type == "video") | "\(.height),\(.codec_name),")
+ (.[] | select(.codec_type == "audio") | "\(.codec_name)")
It does produce the desired output, and it does match the problem description and sample code, but if (for example) there are multiple video objects, or no audio object, the results may not be exactly what you want.
as an alternative, one could use walk-path based unix utility jtc to select required records:
bash $ <video.json jtc -w'[codec_type]:<video>[-1][height]<H>v[-1][codec_name]<C>v[-2][codec_type]:<audio>[-1][codec_name]' -T'"{H},{C},{}"'
"1080,h264,aac"
bash $
PS> Disclosure: I'm the creator of the jtc - shell cli tool for JSON operations