Skip to content

Instantly share code, notes, and snippets.

@bubenkoff
Forked from wjlroe/README.md
Last active June 25, 2018 14:46
Show Gist options
  • Save bubenkoff/0b875d113c20ab3a4ea06b36205f7fbc to your computer and use it in GitHub Desktop.
Save bubenkoff/0b875d113c20ab3a4ea06b36205f7fbc to your computer and use it in GitHub Desktop.

Revisions

  1. bubenkoff revised this gist Apr 22, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion json-to-csv.py
    Original file line number Diff line number Diff line change
    @@ -16,7 +16,7 @@
    data = json.loads(jsonstr)

    # print options
    root_obj = data[options.root]
    root_obj = data[options.root] if options.root else data
    # print root_obj

    headers = options.output.split(',')
  2. @wjlroe wjlroe revised this gist Nov 20, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.md
    Original 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 `
    * 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
  3. @wjlroe wjlroe created this gist Nov 20, 2012.
    23 changes: 23 additions & 0 deletions README.md
    Original 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
    ```
    26 changes: 26 additions & 0 deletions json-to-csv.py
    Original 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)