skip to Main Content

I have a huge file with data in the below format. (It’s the response from an API call I made to one of Twitter’s APIs). I want to extract the value of the field “followers_count” from it. Ordinarily, I would do this with jq with the following command : cat | jq -r ‘.followers_count’

But this contains special characters so jq cannot handle it. Can someone help by telling me how do I convert it in JSON (e.g. using a shell script) or alternatively how to get the followers_count field without conversion? If this format has a specific name, I would be interested to know about it.
Thanks.

SAMPLE LINE IN FILE:

b'[{“id”:2361407554,”id_str”:”2361407554″,”name”:”hakimo ait”,”screen_name”:”hakimo_ait”,”location”:””,”description”:””,”url”:null,”entities”:{“description”:{“urls”:[]}},”protected”:false,”followers_count”:0,”friends_count”:6,”listed_count”:0,”created_at”:”Sun Feb 23 19:08:04 +0000 2014″,”favourites_count”:0,”utc_offset”:null,”time_zone”:null,”geo_enabled”:false,”verified”:false,”statuses_count”:1,”lang”:”fr”,”status”:{“created_at”:”Sun Feb 23 19:09:21 +0000 2014″,”id”:437665498961293312,”id_str”:”437665498961293312″,”text”:”c.ronaldo”,”truncated”:false,”entities”:{“hashtags”:[],”symbols”:[],”user_mentions”:[],”urls”:[]},”source”:”u003ca href=”https://mobile.twitter.com” rel=”nofollow”u003eMobile Web (M2)u003c/au003e”,”in_reply_to_status_id”:null,”in_reply_to_status_id_str”:null,”in_reply_to_user_id”:null,”in_reply_to_user_id_str”:null,”in_reply_to_screen_name”:null,”geo”:null,”coordinates”:null,”place”:null,”contributors”:null,”is_quote_status”:false,”retweet_count”:0,”favorite_count”:0,”favorited”:false,”retweeted”:false,”lang”:”es”},”contributors_enabled”:false,”is_translator”:false,”is_translation_enabled”: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_image_url”:”http://abs.twimg.com/sticky/default_profile_images/default_profile_normal.png”,”profile_image_url_https”:”https://abs.twimg.com/sticky/default_profile_images/default_profile_normal.png”,”profile_link_color”:”1DA1F2″,”profile_sidebar_border_color”:”C0DEED”,”profile_sidebar_fill_color”:”DDEEF6″,”profile_text_color”:”333333″,”profile_use_background_image”:true,”has_extended_profile”:false,”default_profile”:true,”default_profile_image”:true,”following”:false,”follow_request_sent”:false,”notifications”:false,”translator_type”:”none”}]’

2

Answers


  1. This is not the valid JSON, if you want to grab some certain part from this response, you can dump this result in file and then iterate over it and get the text you want to grab.
    otherwise, if response will be in JSON, it will be easily parse through jq library, you can also dump this record in file, convert it into json and then parse it !
    there are multiple ways ‘grep,awk,sed’ ….. you can go for it !

    remove ‘b from beginning and ‘ from bottom,it will become valid JSON !

    Login or Signup to reply.
  2. Well i have removed the b’ from beginning and ‘ from the bottom ! and look it is a valid JSON, now we can easily use jq with it like this !
    i am doing it with my file….

    jq -r '.accounts|keys[]' ../saadaccounts.json | while read key ; 
    do
       DATA="$(jq ".accounts       [$key]" ../saadaccounts.json )"
       FNAME=$( echo $DATA  | jq -r '.first_name' )
       LNAME=$( echo $DATA  | jq -r '.Last_name'  )
    done
    
    *** YOUR JSON FILE *** 
    
    [  
     {
      "id":2361393867,
      "id_str":"2361393867",
      "name":"graam a7bab",
      "screen_name":"bedoo691",
      "location":"",
      "description":"u0627u0633u062au063au0641u0631u0627u0644u0644u0647 u0648u0627u062au0648u0628 u0627u0644u064au0647u0647 ..!*",
      "url":null,
      "entities":{
         "description":{
            "urls":[
    
            ]
         }
      },
      "protected":false,
      "followers_count":1,
      "friends_count":6,
      "listed_count":0,
      "created_at":"Sun Feb 23 19:03:21 +0000 2014",
      "favourites_count":1,
      "utc_offset":null,
      "time_zone":null,
      "geo_enabled":false,
      "verified":false,
      "statuses_count":7,
      "lang":"ar",
      "status":{
         "created_at":"Tue Mar 04 16:07:44 +0000 2014",
         "id":440881284383256576,
         "id_str":"440881284383256576",
         "text":"@Naif8989",
         "truncated":false,
         "entities":{
            "hashtags":[
    
            ],
            "symbols":[
    
            ],
            "user_mentions":[
               {
                  "screen_name":"Naif8989",
                  "name":"u200f naif alharbi",
                  "id":540343286,
                  "id_str":"540343286",
                  "indices":[
                     0,
                     9
                  ]
               }
            ],
            "urls":[
    
            ]
         },
         "source":"u003ca href="http://twitter.com/download/android" rel="nofollow"u003eTwitter for Androidu003c/au003e",
         "in_reply_to_status_id":437675858485321728,
         "in_reply_to_status_id_str":"437675858485321728",
         "in_reply_to_user_id":2361393867,
         "in_reply_to_user_id_str":"2361393867",
         "in_reply_to_screen_name":"bedoo691",
         "geo":null,
         "coordinates":null,
         "place":null,
         "contributors":null,
         "is_quote_status":false,
         "retweet_count":0,
         "favorite_count":0,
         "favorited":false,
         "retweeted":false,
         "lang":"und"
      },
      "contributors_enabled":false,
      "is_translator":false,
      "is_translation_enabled":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_image_url":"http://pbs.twimg.com/profile_images/437664693373911040/ydODsIeh_normal.jpeg",
      "profile_image_url_https":"https://pbs.twimg.com/profile_images/437664693373911040/ydODsIeh_normal.jpeg",
      "profile_link_color":"1DA1F2",
      "profile_sidebar_border_color":"C0DEED",
      "profile_sidebar_fill_color":"DDEEF6",
      "profile_text_color":"333333",
      "profile_use_background_image":true,
      "has_extended_profile":false,
      "default_profile":true,
      "default_profile_image":false,
      "following":false,
      "follow_request_sent":false,
      "notifications":false,
      "translator_type":"none"
      }
    ]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search