command line - How to lift the value of a JSON object that is nested two levels deep? -


given following test.json received response pocket api,

{ "complete": 1, "error": null, "list": {     "1000055792": {         "excerpt": "some text",         "favorite": "0",         "given_title": "some title",         "given_url": "some url",         "has_image": "0",         "has_video": "0",         "is_article": "1",         "is_index": "0",         "item_id": "1000055792",         "resolved_id": "1000055792",         "resolved_title": "title",         "resolved_url": "some url",         "sort_id": 700,         "status": "1",         "time_added": "1438646514",         "time_favorited": "0",         "time_read": "1439025088",         "time_updated": "1439025090",         "word_count": "10549"     },     "1000102810": {         "excerpt": "some text",         "favorite": "0",         "given_title": "title",         "given_url": "some url",         "has_image": "1",         "has_video": "0",         "is_article": "1",         "is_index": "0",         "item_id": "1000102810",         "resolved_id": "1000102810",         "resolved_title": "title",         "resolved_url": "resolved url",         "sort_id": 650,         "status": "1",         "time_added": "1440303789",         "time_favorited": "0",         "time_read": "1440320729",         "time_updated": "1440320731",         "word_count": "3219"     } 

how can access values of keys resolved_title , word_count. nested inside object number, same id, in nested inside list. i've searched , found way access nested objects using jq. how can access values nested inside object within main list object?

also, ids different , not sequential, don't think recursion possible, wrong. i'm intending data extract resolved_title , word_count values each item , save them two-column spreadsheet.

thanks in advance!

you can use .[] operator iterate on elements in array (or in case keys). following give output each field on separate line:

cat <file_with_json> | jq '.list | .[] | .resolved_title, .word_count' 

the first filter operates on list element. second filter says for every element , output resolved_title , .word_count fields. produces following:

"title" "3219" "title" "10549" 

Comments