4

I currently have a json file that looks like this....

{
    "data": [
    {
        "tag": "cashandequivalents",
        "value": 10027000000.0
    },
    { 
        "tag": "shortterminvestments",
        "value": 101000000.0
    },
    {
        "tag": "accountsreceivable",
        "value": 4635000000.0
    },
    {
        "tag": "netinventory",
        "value": 1386000000.0
    }...

but what I am trying to get to is this

{ 
    "cashandequivalents": 10027000000.0, 
    "shortterminvestments":101000000.0 , 
    "accountsreceivable":4635000000.0,  
    "netinventory":1386000000.0
}

I just don't know how to go about this.


Maybe there is an easier way, but this seems the most logical to me because the next step is writer.writerow to csv

So eventually the csv will look like

cashandequivalents | shortterminvestments | accountsreceivable | netinventory
100027000000         101000000000           46350000000          13860000000
###########          ############           ###########          ...........

(writer.writeheader will be done outside of the loop so I am only writing the values, not the "tags")

Thanks

1
  • 1
    combined = {item['tag']: item['value'] for item in a['data']} where a is your initial structure. That's a dictionary comprehension. Commented Apr 17, 2017 at 17:59

2 Answers 2

4

A naive solution:

import json

json_data = {
    "data": [
        {
            "tag": "cashandequivalents",
            "value": 10027000000.0
        },
        {
            "tag": "shortterminvestments",
            "value": 101000000.0
        },
        {
            "tag": "accountsreceivable",
            "value": 4635000000.0
        },
        {
            "tag": "netinventory",
            "value": 1386000000.0
        }
    ]
}

result = dict()
for entry in json_data['data']:
    result[entry['tag']] = entry['value']

print json.dumps(result, indent=4)

Output

{
    "shortterminvestments": 101000000.0, 
    "netinventory": 1386000000.0, 
    "accountsreceivable": 4635000000.0, 
    "cashandequivalents": 10027000000.0
}
Sign up to request clarification or add additional context in comments.

Comments

2

The easiest and cleanest way to do this is with a dictionary comprehension.

d = {
 "data": [
  {
  "tag": "cashandequivalents",
  "value": 10027000000.0
  },
  { 
  "tag": "shortterminvestments",
  "value": 101000000.0
  },
  {
  "tag": "accountsreceivable",
  "value": 4635000000.0
   },
   {
   "tag": "netinventory",
   "value": 1386000000.0
   }
 ]
}
newDict = {i['tag']: i['value'] for i in d['data']}

# {'netinventory': 1386000000.0, 'shortterminvestments': 101000000.0, 'accountsreceivable': 4635000000.0, 'cashandequivalents': 10027000000.0}

This iterates through the list that is contained within the "data" key of your original dictionary and creates a new one inline with the key being the tag value of each and the value being the value for each during the iterations.

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.