2016-07-05 52 views
0

我有一些大型的json文件,我需要能夠在終端中快速讀取。我有興趣訪問每個JSON的最後一個元素,其中,例如,可能是這樣的屬性:在終端中訪問json的元素

 }, 
     "source": "<a href=\"http://twitter.com/download/android\" 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": "http://abs.twimg.com/images/themes/theme1/bg.png", 
      "profile_background_image_url_https": "https://abs.twimg.com/images/themes/theme1/bg.png", 
      "profile_background_tile": false, 
      "profile_banner_url": "https://pbs.twimg.com/profile_banners/2479274491/1453298552", 
      "profile_image_url": "http://pbs.twimg.com/profile_images/748055063625605120/rByPUFsn_normal.jpg", 
      "profile_image_url_https": "https://pbs.twimg.com/profile_images/748055063625605120/rByPUFsn_normal.jpg", 
      "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 
     } 
    } 
] 

什麼將是終端訪問JSON文件的最後一個元素,並看到正確的命令例如,屬性值爲"geo_enabled"

回答

0

jq命令可能是你的朋友。

在緊要關頭:

jq .user.geo_enabled 

這將是一個稍長的路徑,給你的JSON結構。可能是這樣的:

jq .[0].parent.user.geo_enabled 

(其中'父'是任何''上面'您的用戶密鑰)。

+0

如何在文件上調用jq,讓我們假設該文件是否被稱爲x.json? –

+0

'jq .user.geo_enabled x.json' – Sobrique