0

I have the response message below and code which I follow

result= a["{\"msg\":\"result\",\"id\”:\”test\”,\”result\":{\"categories\":[\"<20M\",\"20M+\",\"50M+\",\"300M+\",\"1G\"],\"series\":[{\"name\":\"Fiber\",\"data\":[216,41,10393,37394,6016]}],\"totals\":{\"<20M\":216,\"20M+\":41,\"50M+\":10393,\"300M+\":37394,\"1G\":6016}}}"]

#Remove a
     sliceresult=result[1:]
    print('Result After slice: {}'.format(sliceresult))
    string_input = ''.join(str(s) for s in sliceresult)
    print(string_input)
    category = json.loads(string_input)['result']['categories']
    series = json.loads(string_input)['result']['series']
    total = json.loads(string_input)['result']['totals']
    print(category, series, total)
    Error Response was:
        category = json.loads(result[1:])['result']['categories']
    TypeError: list indices must be integers, not str

1 Answers1

1

This might help you to begin with:

 a = ["{\"msg\":\"result\",\"id\":\"test\",\"result\":{\"categories\":[\"<20M\",\"20M+\",\"50M+\",\"300M+\",\"1G\"],\"series\":[{\"name\":\"Fiber\",\"data\":[216,41,10393,37394,6016]}],\"totals\":{\"<20M\":216,\"20M+\":41,\"50M+\":10393,\"300M+\":37394,\"1G\":6016}}}"]

string_input = ''.join(str(s) for s in a)
print(string_input)

    category = json.loads(string_input)['result']['categories']
    series = json.loads(string_input)['result']['series']
    total = json.loads(string_input)['result']['totals']
    print(category, series, total)

Also be careful, your input string is not properly formatted. it should be " not

change198
  • 1,647
  • 3
  • 21
  • 60
  • this one works fine but how should I remove '[ ]' square brackets in the response – Mahendra Vengalam Jun 10 '20 at 12:44
  • 1
    then in your case it's not json , it's a list. I have modified my answers, that will definitely help. – change198 Jun 10 '20 at 12:55
  • # Removing a from the string sliceresult=result[1:] print('Result After slice: {}'.format(sliceresult)) string_input = ''.join(str(s) for s in sliceresult) print(string_input) category = json.loads(string_input)['result']['categories'] series = json.loads(string_input)['result']['series'] total = json.loads(string_input)['result']['totals'] print(category, series, total) – Mahendra Vengalam Jun 10 '20 at 13:19
  • I followed this code and got an error was category = json.loads(string_input)['result']['categories'] TypeError: list indices must be integers, not str – Mahendra Vengalam Jun 10 '20 at 13:20
  • I edited latest code, could u please review it because in my response 'a' also included – Mahendra Vengalam Jun 10 '20 at 13:40
  • I can't review it and be helpful here, because if I try to replicate and add `a` next to`[]`, in python you will get unresolved reference for `a`. To help you have to tell me what is `a`? – change198 Jun 10 '20 at 15:15
  • in websocket response 'a' also included because a indicates active state – Mahendra Vengalam Jun 11 '20 at 05:22