0

I'm making a script for the game and i need help about grabbing dictionary information out of json.

JSON

So far I've dealt with list and I didn't had problem grabbing that information.

req['result']['trades'][key]['output'] 

This is my try of grabbing the info after which i get error list indices must be integers, not dict.

Any help greatly appreciated.

json print, i have removed some account sensitive data.

{
    'fue_step': 30,
    'type': 'm',
    'rubies': xxx, 
    'might': xxx,
    'race': 'xxx',
    'result': {
        'trades': [
            {
                'id': 13,
                'requirement': ['VenomTrapPart', 1],
                'token_cost': 1,
                'output': ['VulcansBlessingRare', 1],
                'name': 'VenomVulcan',
                'enabled': True
            }, 
            {
                'id': 14,
                'requirement': ['EternalRune', 50],
                'token_cost': 1,
                'output': ['ColossalMite500TroopPrizeItem', 1],
                'name': 'EternalColossal',
                'enabled': True
            }, 
            {
                'id': 86,
                'requirement':['RenameProclamation', 25],
                'token_cost': 1,
                'output': ['TimeTrickstersBag', 1],
                'name': 'RenameTrick',
                'enabled': True
            },
            {
                'id': 95,
                'requirement': ['FireDragonBlaze', 1],
                'token_cost': 1,
                'output': ['FireTroop1000TroopPrizeItem', 10],
                'name': 'BlazeFireTroop',
                'enabled': True
            },
            {
                'id': 100,
                'requirement': ['StoneDragonQuake', 1],
                'token_cost': 1,
                'output': ['StoneTroop10kTroopPrizeItem', 1],
                'name': 'StoneDQStoneTroop',
                'enabled': True
            },
            {
                'id': 113,
                'requirement': ['VulcansBlessing15Elite', 3],
                'token_cost': 1,
                'output': ['VulcansBlessing16Elite', 1],
                'name': '15 --> 16 Elite',
                'enabled': True
            },
            {
                'id': 114,
                'requirement': ['LunaPowder', 25],
                'token_cost': 1,
                'output': ['IceTroop50kTroopPrizeItem', 1],
                'name': 'Luna Powder --> Soul Reapers',
                'enabled': True
            },
            {
                'id': 115,
                'requirement': ['GreaterCrystalDefense', 5],
                'token_cost': 1,
                'output': ['MasterCrystalDefense', 1],
                'name': 'Greater Defense --> Master Defense',
                'enabled': True
            },
            {
                'id': 116,
                'requirement': ['NanoCanisters', 3],
                'token_cost': 1,
                'output': ['TestroniusDeluxe', 1],
                'name': 'Nano Canisters for Deluxes',
                'enabled': True
            },
            {
                'id': 117,
                'requirement': ['VulcansBlessing16Common', 3],
                'token_cost': 1,
                'output': ['VulcansBlessing17Common', 1],
                'name': "Common +16's for Common +17",
                'enabled': True
            }
        ], 
        'trades_today': 0,
        'doubloons_left': 9567,
        'free_trades_left': 1,
        'success': True,
        'auto_confirm': True,
        'trade_reset_date':1450771200
    },
    'client_cachebreaker': '1449879464'
Jason
  • 2,725
  • 2
  • 14
  • 22
Orion1125
  • 95
  • 1
  • 2
  • 9
  • There are 4 things we need to know the data-structure of first: – abe Dec 19 '15 at 23:04
  • ['result']['trades'][key]['output'] each individually. – abe Dec 19 '15 at 23:04
  • 1
    The hierarchy in the image you posted is difficult to understand. Can you `print` the JSON instead and post that? – Jason Dec 19 '15 at 23:14
  • 1
    Have you tried `d = json.loads(input_string)` (or `json.load` if reading form a file directly) – Pynchia Dec 19 '15 at 23:34
  • If the error message tells you that the interpreter thinks you have a `list` there, then that's what you have. – TigerhawkT3 Dec 20 '15 at 00:32
  • @Jason I have done json.loads and it successfully loads json file. It is in form of a dictionary and that's what makes it the problem for me. So far i have only worked with lists and grabbing information from them was easy. 1st item under output is item and 2nd is value(amount), the thing i need to do is somehow call them and use further in script. I will post json in question. – Orion1125 Dec 22 '15 at 00:48

1 Answers1

1

I edited your edit to include indentation in your JSON so that it is more readable. As soon as the edit is approved, you'll be able to see it.

Here is an example of how to access a value deep within the object:

print req['result']['trades'][4]['name']

Output:

StoneDQStoneTroop

If you want to access dicts within a list by their values, here is a post that explains a few ways to do so. This would allow you, for example, to find a dict in req['result']['trades'] by its 'id' value instead of by its index within the 'trades' list.

Community
  • 1
  • 1
Jason
  • 2,725
  • 2
  • 14
  • 22
  • Thank you, i will read it. Values like name, token cost, id i know how to get. Dict is a problem so thanks a lot for your help. – Orion1125 Dec 22 '15 at 02:18