1

I am calling an API and it returns the value to me as a Json object, like below:

{
    "indicator_value": {
        "AFG": {
            "137506": {
                "2002": 0.373,
                "2003": 0.383,
                "2004": 0.398,
                "2005": 0.408,
                "2006": 0.417,
                "2007": 0.429,
                "2008": 0.437,
                "2009": 0.453,
                "2010": 0.463,
                "2011": 0.471,
                "2012": 0.482,
                "2013": 0.487,
                "2014": 0.491,
                "2015": 0.493,
                "2016": 0.494,
                "2017": 0.498
            }
        },
        "AGO": {
            "137506": {
                "1999": 0.374,
                "2000": 0.387,
                "2001": 0.401,
                "2002": 0.418,
                "2003": 0.429,
                "2004": 0.442,
                "2005": 0.455,
                "2006": 0.471,
                "2007": 0.492,
                "2008": 0.502,
                "2009": 0.522,
                "2010": 0.52,
                "2011": 0.534,
                "2012": 0.543,
                "2013": 0.554,
                "2014": 0.564,
                "2015": 0.572,
                "2016": 0.577,
                "2017": 0.581
            }
        },

The file continues like that and then after the indicator_value array there are two other arrays I am not interested in.

Can anyone advise as to how I, using python, transform this to a csv file with the format:

indicator | country | year | value
 HDI      | AFG     | 2017 | 0.498
 HDI      | AFG     | 2016 | 0.494
3
  • Hi, you should take a look at json package first to parse your input data. Then check out the csv package to generate a CSV file. Commented Oct 4, 2019 at 14:21
  • What have you tried so far? Commented Oct 4, 2019 at 14:24
  • 2
    Possible duplicate of How can I convert JSON to CSV? Commented Oct 4, 2019 at 14:28

1 Answer 1

2

This might work, depending on what the rest of your data looks like:

import pandas as pd

rows = []
for k1, v1 in data['indicator_value'].items():
    row = ['HDI', k1]
    for k2, v2 in v1.items():
        for k3, v3 in v2.items():
            rows.append(row + [k3, v3])

df = pd.DataFrame(rows, columns=['indicator', 'country', 'year', 'value'])

where df is a pandas DataFrame and df.head() now looks like:

pandas output

Now you can do df.to_csv(fname).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.