-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

3
  • Have you tried using json? Commented Jul 30, 2016 at 8:08
  • Well i am receiving Json, convert it to a dict so i can work with it and then convert it back to json. Commented Jul 30, 2016 at 9: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 minimal reproducible example. And please don't put noise like thank yous in your posts; I edited it out for a reason. Commented Jul 30, 2016 at 9:42

1 Answer 1

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

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.