Created
March 26, 2015 15:48
-
-
Save scottrogowski/2047ab8160fef080c270 to your computer and use it in GitHub Desktop.
Command line tool to load a json url endpoint into a python datastructure
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 | |
# Loads json into a python datastructure from a url | |
# Usage: curljson http://example.com/json_endpoint | |
# Then the datastructure is accessible via jobj | |
import json | |
import sys | |
from IPython import embed | |
from subprocess import Popen, PIPE | |
def load_jobj(*args): | |
if len(args) == 1: | |
args = args[0].split() | |
if args[0] != 'curl': | |
args = ['curl'] + args | |
if len(args) > 1 and args[1] == 'curl': | |
args.pop(1) | |
p = Popen(args, stdout=PIPE, stderr=PIPE) | |
stdout, stderr = p.communicate() | |
if stderr: | |
print stderr | |
return json.loads(stdout) | |
if __name__ == "__main__": | |
args = list(sys.argv) | |
args[0] = 'curl' | |
jobj = load_jobj(*args) | |
print "="*50 | |
print "The json is loaded as 'jobj'. Have at it boss" | |
print "="*50 | |
embed() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment