1

Newbie python programmer here, I have the following json response:

[
  {
    "type": "Incursion",
    "state": "mobilizing",
    "influence": 1,
    "has_boss": true,
    "faction_id": 500019,
    "constellation_id": 20000739,
    "staging_solar_system_id": 30005054,
    "infested_solar_systems": [
      30005050,
      30005051,
      30005052,
      30005053,
      30005054,
      30005055
    ]
  },
  {
    "type": "Incursion",
    "state": "established",
    "influence": 0,
    "has_boss": false,
    "faction_id": 500019,
    "constellation_id": 20000035,
    "staging_solar_system_id": 30000248,
    "infested_solar_systems": [
      30000244,
      30000245,
      30000246,
      30000247,
      30000248,
      30000249,
      30000250,
      30000251,
      30000252,
      30000253
    ]
  },
  {
    "type": "Incursion",
    "state": "mobilizing",
    "influence": 0,
    "has_boss": false,
    "faction_id": 500019,
    "constellation_id": 20000161,
    "staging_solar_system_id": 30001101,
    "infested_solar_systems": [
      30001097,
      30001098,
      30001099,
      30001100,
      30001101,
      30001102
    ]
  },
  {
    "type": "Incursion",
    "state": "established",
    "influence": 0,
    "has_boss": false,
    "faction_id": 500019,
    "constellation_id": 20000647,
    "staging_solar_system_id": 30004434,
    "infested_solar_systems": [
      30004425,
      30004426,
      30004427,
      30004428,
      30004429,
      30004430,
      30004431,
      30004432,
      30004433,
      30004434,
      30004435
    ]
  },
  {
    "type": "Incursion",
    "state": "established",
    "influence": 0.061500001698732376,
    "has_boss": false,
    "faction_id": 500019,
    "constellation_id": 20000570,
    "staging_solar_system_id": 30003910,
    "infested_solar_systems": [
      30003904,
      30003906,
      30003908,
      30003909,
      30003910,
      30003903
    ]
  }
]

The original code was written to parse an XML reponse.

This is the code in question:

incursion_constellations = []

if (online):
    inc = urllib2.urlopen('https://esi.tech.ccp.is/latest/incursions/')
else:
    inc = file(r'incursions.json', 'r')

jinc = json.load(inc)

for j in jinc['items']:
    incursion_constellations.append(str(j['constellation']['id_str']))


for s in all_stations:
    cur.execute("SELECT constellationID FROM mapSolarSystems WHERE solarSystemID = " + str(s['ssid']))
    res = cur.fetchone()
    cid = str(res[0])
    s['incursion'] = cid in incursion_constellations

The area I have having a hard time understanding is this: for j in jinc['items']:

I am getting this error:

Traceback (most recent call last):
  File "./stations.py", line 201, in <module>
    for j in jinc['items']:
TypeError: list indices must be integers, not str

Can anyone help me understand how to convert this into being able to parse the json response and retrieve the constellation_id and append it to a list?

Thanks in advance.

4
  • 2
    Where do you see the key called "items" in the JSON? Commented Aug 1, 2017 at 19:14
  • 1
    And, where are the keys "constellation" and "id_str"? Commented Aug 1, 2017 at 19:15
  • @IgnacioVazquez-Abrams - I don't. The original code was reading and parsing an XML reponse, and the developer of the API changed it to a json response. Commented Aug 1, 2017 at 19:25
  • @JaredGoguen Those were from the original XML reponse the api returned. Commented Aug 1, 2017 at 19:25

2 Answers 2

2

Change your original loop to:

for j in jinc:
    incursion_constellations.append(str(j['constellation_id']))

But you need to be sure that constellation_id in json is the same id that was under ['constellation']['id_str'] previously

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

1 Comment

That did it, thank you very much. I have a better understanding of what was needed to do now.
1

Seeing the [ and ] at the beginning and the end of the response, it seems like this json response is list, not a dict, just as your error is suggesting.

If it is a list, you should be using integer as index, instead of str, like you'd do in dict. Hence, your code should be something like

jinc[0]['constellation_id']

(I don't see where the ['constellation']['id_str'] part comes from)

whatever goes inside the [ and ] is in a list, and should be using an integer index. the ones in { and } are in dict, and should use str index.

to loop through it, just use range and len.

a similar question has been answered here.

2 Comments

The 'item' was from the original XML response. I am trying, unsuccessfully, to convert this code to python, in which I have never written before, I am a PHP programmer.
This isn't in any way a Python-specific issue: even in PHP, you can't access a member of a list using a non-numeric string as the index. So the application logic is clearly not matching here -- maybe you're iterating over list elements in the PHP but dropped that from the Python?

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.