-1

Unable to parse data

I just printed data = json.loads(somepostmessage)

Output of data:

{'number': 'INC0010021', 'title': 'ghjghjgh', 'description': 'test creating sericenow tickets through post', 'state': 'New', 'priority': 'Planning', 'assigned_to': '', 'caller_id': ''}

Code:

 if request.method == 'POST':
    print(request.body)
    print(json.loads(request.body))
    print(type(json.loads(request.body)))
    data = json.loads(request.body)
    print(data['number'])
    return Response({"message": "Data mismatching"})

I was trying to parse through it data['number']:

Error: String indices must be integers
martineau
  • 119,623
  • 25
  • 170
  • 301
Vyshnavi
  • 149
  • 2
  • 10
  • Has your data variable loaded as a dictionary or a string? What does `type(data)` give you? Have a look at [this answer](https://stackoverflow.com/a/20085477/6144626) to get a better understanding of how to read json data using that library. – Ari Cooper-Davis Mar 25 '19 at 11:47
  • Share your code please. – balderman Mar 25 '19 at 12:09
  • hi @AriCooper-Davis Type is – Vyshnavi Mar 25 '19 at 12:21
  • hi @balderman if request.method == 'POST': print(request.body) print(json.loads(request.body)) print(type(json.loads(request.body))) return Response({"message": "Data mismatching"}) – Vyshnavi Mar 25 '19 at 12:21
  • Vyshnavi: Please [edit] your question and add the code to it. – martineau Mar 25 '19 at 12:27
  • @martineau can you check once I edited the code – Vyshnavi Mar 25 '19 at 12:29
  • Vyshnavi: I don't think the code was formatting properly and you deleted some important information. I tried to correct both of these issues, but am not sure about the code because it doesn't make sense (so I may have indented it wrong). I also don't understand how you can show the data output but claim the `loads()` is causing the error. The output is Python syntax, not proper JSON. – martineau Mar 25 '19 at 12:40
  • What is the output of `print(type(json.loads(request.body)))` ? I believe the answer is 'str' and not 'dict' – balderman Mar 25 '19 at 12:43
  • what is the output of `request.POST` – Jeril Mar 25 '19 at 12:45

1 Answers1

0

It finally dawned on me what's wrong — it's because the json.loads(request.body) is returning a string, not a dictionary, which you assign to variable data. Following that you try to access data['number'], but since data is a string not a dictionary which cannot be indexed with a string like the former can be.

It seems strange (and might be a security risk, so there's probably a better way), but this seems like it might work (untested):

import ast
...
data = ast.literal_eval(json.loads(request.body))
martineau
  • 119,623
  • 25
  • 170
  • 301
  • json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 26 (char 25) – Vyshnavi Mar 25 '19 at 12:55
  • Vyshnavi: Hmm, there's no double quotes in the data output shown in your question. Please edit your question again and replace it with what `print(request.body)` displays. – martineau Mar 25 '19 at 13:01