auser auser - 6 months ago 21
JSON Question

Parsing JSON with UNIX tools

I'm trying to parse JSON returned from a curl request, like so:

curl 'http://twitter.com/users/username.json' |
sed -e 's/[{}]/''/g' |
awk -v k="text" '{n=split($0,a,","); for (i=1; i<=n; i++) print a[i]}'


The above splits the JSON into fields, for example:

% ...
"geo_enabled":false
"friends_count":245
"profile_text_color":"000000"
"status":"in_reply_to_screen_name":null
"source":"web"
"truncated":false
"text":"My status"
"favorited":false
% ...


How do I print a specific field (denoted by the
-v k=text
)?

Answer

I've never used it, but you could try out jsawk. It would be something like this (haven't tested this, so I may be wrong):

curl 'http://twitter.com/users/username.json' | jsawk -a 'return this.name'