Last active
December 30, 2016 17:32
-
-
Save renanivo/983c745615cb9ea35bc4f675ce913903 to your computer and use it in GitHub Desktop.
Reads a JSON from STDIN and prints a python dict
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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