-2

After making a post and receiving the return I want to treat him. lambdaGetDependencies returns a JSON and I need specific values from the JSON, that's what I try to do inside the cycle.

if CLUSTER_ENDPOINT and CLUSTER_PORT:
    data = lambdaGetDependencies(resource)
else:
    print("provide CLUSTER_ENDPOINT and CLUSTER_PORT environment variables")
    elements = data['result']['data']['@value']
    dependencies = [None] * len(elements)
    count = 0
    j = 0
    for i in elements:
        while j < len(i['@value']):
            x = {i['@value'][j]: i['@value'][j+1]['@value'][0]}
            c.append(x)
            dependencies[count] = i['@value'][j+1]['@value'][0]
            count += 1
            j += 1
    return json.dumps(c)

The problem is that I get string indices must be integers on the line:

x = {i['@value'][j]: i['@value'][j+1]['@value'][0]}  

and I don't get why. Any ideas?

Evhz
  • 8,852
  • 9
  • 51
  • 69
Raphael
  • 57
  • 6
  • That entirely depends on what `data['result']['data']['@value']` is, but that wasn't provided. In general it just means that your program isn't looking at the correct part of the JSON at that particular point in time - `print()` and IDE breakpoints can be helpful here for debugging, – alex Sep 20 '19 at 13:40
  • Thanks! But if I do print(i['@value'][j]['@value'][0]) and print(i['@value'][j+1]['@value'][0]) it works – Raphael Sep 20 '19 at 13:51
  • 2
    It's probably an issue with `i['@value'][j]` then. It's impossible to say unless you provide the actual data structure you're working with - nobody can reproduce the error without seeing the JSON. See https://stackoverflow.com/questions/6077675/why-am-i-seeing-typeerror-string-indices-must-be-integers for more details – alex Sep 20 '19 at 14:00

1 Answers1

0

i['@value'][j+1] is probably a string and not a dictionary. If that is the case then this i['@value'][j+1]['@value'][0] is invalid.

  • Thank you for the reply but do you have a solution? I'm aware the issue is the "j+1" – Raphael Sep 20 '19 at 13:51
  • That depends on what you are trying to achieve. – Nikolay Hüttenberend Sep 20 '19 at 13:55
  • I want to go through the json – Raphael Sep 20 '19 at 13:56
  • 1
    well, you are doing that already. You need to be more specific. – Nikolay Hüttenberend Sep 20 '19 at 13:56
  • Sorry but I don't know how to be more specific... I want to access a value in json and the way I write it is wrong but the logic is good – Raphael Sep 20 '19 at 14:00
  • 2
    You are accessing the values and the way is not wrong. The problem is that depending on the structure of the data you may have to do different things, as alex pointed out in his comment above. I don't know what the data is so I can't tell you anything. You said you did a print and it worked, right? Why not post the result of the print? – Nikolay Hüttenberend Sep 20 '19 at 14:03
  • 1
    Given that you are creating a single entry `dict` in the line `x = {i ...` and afterwards converting back to `json`, then the key to the dict *must* be a string. Therefore `i['@value'][j+1]` must also be a string as the next element in the `while` loop. – quamrana Sep 20 '19 at 14:06
  • Thanks a lot I know what I have to do – Raphael Sep 20 '19 at 15:12