-2

I have this huge JSON file, which contains more than 80000 lines, looking like that:

{u'hero_name': u'bristleback', u'gold_total': 17937, tick:24098}
{u'hero_name': u'silencer', u'gold_total': 10847, tick:24198}
{u'hero_name': u'silencer', u'gold_total': 11789, tick:25098}
{u'hero_name': u'weaver', u'gold_total': 27084, tick:27098}

There are 10 different heroes with their gold data. I want to sort that data for the hero names, make a json list of the gold data, and then store it in a databse table looking like that, where gold_data should be like [{tick, gold}, {tick, gold}, ...]

id | hero_name | gold_data (json field) | ... 

But seems like i am too stupid to sort that. I tried building a list out of it and using sorted(gold_list, key=lambda s: s[0])

I am just not too comfortable with Python yet and I tried so many different things, but i have no idea how to sort,and then split this dictionary for the hero_names key so I can get that JSON object for gold_data

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
  • Have you tried using [`json`](https://docs.python.org/3/library/json.html)? – jonrsharpe Jul 30 '16 at 08:08
  • Well i am receiving Json, convert it to a dict so i can work with it and then convert it back to json. – Peemo Royal Jul 30 '16 at 09:39
  • 1
    That didn't answer my question. Python has built-in functionality for handling JSON, as linked above, so please look into it and make an effort to use it. Then, if you're still stuck, you'll have an actual [mcve]. And please don't put noise like thank yous in your posts; I edited it out for a reason. – jonrsharpe Jul 30 '16 at 09:42

1 Answers1

0

You need to use the json - there are other packages, but this one should do it.

  1. Load the JSON to a veriable using open()
  2. Convert to dict using json.loads()

For example:

import json
from pprint import pprint

with open('data.json') as data_file:    
    data = json.load(data_file)

dct = json.loads(data)
pprint(dct)

You CANNOT sort the dictionary though - you will have to convert the dict to array of tuples - please, refer here and here and here

Community
  • 1
  • 1
RafazZ
  • 4,049
  • 2
  • 20
  • 39