the_darkside the_darkside - 3 months ago 8
JSON Question

accessing elements of json in terminal

I have a number of large json files that I need to be able to read quickly in the terminal. I am interested accessing attributes in the last element of each json, which, for example, might look like this:

"source": "<a href=\"\" rel=\"nofollow\">Twitter for Android</a>",
"text": "RT @kosaqsi_tweets: Ah ah..!!!",
"truncated": false,
"user": {
"contributors_enabled": false,
"created_at": "Tue May 06 04:48:07 +0000 2014",
"default_profile": true,
"default_profile_image": false,
"description": "",
"entities": {
"description": {
"urls": []
"favourites_count": 2147,
"follow_request_sent": false,
"followers_count": 72,
"following": false,
"friends_count": 207,
"geo_enabled": true,
"has_extended_profile": false,
"id": 2479274491,
"id_str": "2479274491",
"is_translation_enabled": false,
"is_translator": false,
"lang": "en",
"listed_count": 1,
"location": "Singapore",
"name": "karthikeyan vedalam",
"notifications": false,
"profile_background_color": "C0DEED",
"profile_background_image_url": "",
"profile_background_image_url_https": "",
"profile_background_tile": false,
"profile_banner_url": "",
"profile_image_url": "",
"profile_image_url_https": "",
"profile_link_color": "0084B4",
"profile_sidebar_border_color": "C0DEED",
"profile_sidebar_fill_color": "DDEEF6",
"profile_text_color": "333333",
"profile_use_background_image": true,
"protected": false,
"screen_name": "k84362172",
"statuses_count": 1521,
"time_zone": null,
"url": null,
"utc_offset": null,
"verified": false

What would be the proper command in terminal to access the last element of the json file and to see, for example, the value of attribute


The jq command may be your friend.

At a pinch:

jq .user.geo_enabled

It'll be a slightly longer path, given your JSON structure. Probably something like:

jq .[0].parent.user.geo_enabled

(Where 'parent' is whatever is 'above' your user key).