-
-
Save bubenkoff/0b875d113c20ab3a4ea06b36205f7fbc to your computer and use it in GitHub Desktop.
Revisions
-
bubenkoff revised this gist
Apr 22, 2016 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -16,7 +16,7 @@ data = json.loads(jsonstr) # print options root_obj = data[options.root] if options.root else data # print root_obj headers = options.output.split(',') -
wjlroe revised this gist
Nov 20, 2012 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -3,7 +3,7 @@ ## Tools needed * httpie -- `easy_install httpie` (probably you want to `sudo easy_install httpie` * this script -- `curl -o /usr/local/bin/json-to-csv.py https://raw.github.com/gist/4119347/24adb5dcd9c025c272042653a70d25b73b485958/json-to-csv.py` * make that executable -- `chmod +x /usr/local/bin/json-to-csv.py` ## Here we go -
wjlroe created this gist
Nov 20, 2012 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,23 @@ # Howto download a JSON API array and spit out a CSV ## Tools needed * httpie -- `easy_install httpie` (probably you want to `sudo easy_install httpie` * this script -- `curl -o /usr/local/bin/json-to-csv.py ` * make that executable -- `chmod +x /usr/local/bin/json-to-csv.py` ## Here we go This example calls the Altmetric API, asking for the 2 most cited articles in blogs and news and prints a CSV on standard output containing the altmetric_id and score of each article. ``` % http http://api.altmetric.com/v1/citations/1d\?num_results\=2\&cited_in\=blogs,news | python json-to-csv.py -o altmetric_id,score -r results altmetric_id,score 1077374,124.2 1066154,971.274 ``` Save it to a file: ``` % http http://api.altmetric.com/v1/citations/1d\?num_results\=2\&cited_in\=blogs,news | python json-to-csv.py -o altmetric_id,score -r results > top-cited-news-blogs.csv ``` 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,26 @@ #!/usr/bin/env python import sys import json from optparse import OptionParser if __name__ == '__main__': parser = OptionParser() parser.add_option("-o", "--output", action="store", help="Format to output, e.g. -o 'id,name,age,some_other_field'") parser.add_option("-r", "--root", action="store", help="Root JSON object to refer to fields from, e.g. -r 'results'") (options, args) = parser.parse_args() jsonstr = sys.stdin.read() data = json.loads(jsonstr) # print options root_obj = data[options.root] # print root_obj headers = options.output.split(',') print options.output rows = [map(lambda header: str(values[header]), headers) for values in root_obj] for row in rows: print ','.join(row)