Skip to content

Instantly share code, notes, and snippets.

@jsjohnst
Created August 25, 2011 22:27
Show Gist options
  • Save jsjohnst/1172177 to your computer and use it in GitHub Desktop.
Save jsjohnst/1172177 to your computer and use it in GitHub Desktop.
Terrible, but working JSON parser for flat JSON in already validated semantic form.
Prints out each key/value pair:
awk '{ gsub(/[{}]/, "", $0); n=split($0,a,","); for (i=1; i<=n; i++) if(length(k) > 0) { if(index(a[i], k) != 0) { split(a[i],b,":"); print b[2]; } } else { print a[i] } }'
Example input:
{"verified":false,"protected":false,"location":"San Francisco"}
Example output:
"verified":false
"protected":false
"location":"San Francisco"
Here's how you specify which key you want from the JSON tree:
awk -v k=verified '{ gsub(/[{}]/, "", $0); n=split($0,a,","); for (i=1; i<=n; i++) if(length(k) > 0) { if(index(a[i], k) != 0) { split(a[i],b,":"); print b[2]; } } else { print a[i] } }'
Example input:
{"verified":false,"protected":false,"location":"San Francisco"}
Example output:
false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment