Skip to content

Instantly share code, notes, and snippets.

@renanivo
Last active December 30, 2016 17:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save renanivo/983c745615cb9ea35bc4f675ce913903 to your computer and use it in GitHub Desktop.
Save renanivo/983c745615cb9ea35bc4f675ce913903 to your computer and use it in GitHub Desktop.
Reads a JSON from STDIN and prints a python dict
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# vi: set ft=python :
"""
Convert a JSON from stdin in a python dict. Ex:
$ echo '{"foo": "bar"}' | ./json2dict
> {u'foo': u'bar'}
Install:
curl https://gist.githubusercontent.com/renanivo/983c745615cb9ea35bc4f675ce913903/raw/db4adcb07e8ed47a26e3a79271c6425e74bb9ca7/json2dict > json2dict; chmod +x json2dict
"""
import json
import sys
COLOR_FAIL = '\033[91m'
COLOR_END = '\033[0m'
try:
print json.load(sys.stdin)
sys.exit(0)
except ValueError as e:
print '{}Invalid JSON{}'.format(COLOR_FAIL, COLOR_END)
print e
sys.exit(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment