5

When I make use of JSON.dump() I am getting below JSON format

Dumps data"b'{\"AutomaticReadabilityIndex\":2.7999999999999994,\"AgeLevel\":[\" 11 to 12\"],\"Statement\":[\"Nice. Your grade is about six\"],\"SpacheScore\":1.877,\"GunningFogScore\":9.099999999999998,\"SmogIndex\":5.999999999999999}'"

When I make use of JSON.loads() I am getting below JSON format with bytes

loads data b'{"AutomaticReadabilityIndex":2.7999999999999994,"AgeLevel":[" 11 to 12"],"Statement":["Nice. Your grade is about six"],"SpacheScore":1.877,"GunningFogScore":9.099999999999998,"SmogIndex":5.999999999999999}'

My question is when I am using loads format the output has to be in dictionary type but I don't know why I am getting string type as my output. How to convert this JSON string into dictionary type.

Loads Data Type : type of loads <class 'str'>

When I trying to parse string type JSON directly I am getting below error

ERROR : Traceback (most recent call last):
File "Db.py", line 84, in <module>
print(par['GunningFogScore'])
TypeError: string indices must be integers
Madhu
  • 79
  • 1
  • 5
  • 1
    you're dumping a `json` string not a python dict (you're dumping a dump...). Don't dump a string using `json`, just write it. – Jean-François Fabre Jun 15 '17 at 07:11
  • @Jean-FrançoisFabre what does it mean "don't dump a string using json just write it" i'm struggling with the same error but dont get your comment :( – shurrok Oct 31 '17 at 13:25
  • @soommy12 since you were able to dig up the question and you have the same problem, I made an answer then. Tell me if you understand better. – Jean-François Fabre Oct 31 '17 at 13:48

1 Answers1

5

I can reproduce your problem simply with:

import json

s = {"AutomaticReadabilityIndex":2.7999999999999994,"AgeLevel":[" 11 to 12"],"Statement":["Nice. Your grade is about six"],"SpacheScore":1.877,"GunningFogScore":9.099999999999998,"SmogIndex":5.999999999999999}

print(json.dumps(json.dumps(s)))

result:

"{\"SmogIndex\": 5.999999999999999, \"AutomaticReadabilityIndex\": 2.7999999999999994, \"SpacheScore\": 1.877, \"GunningFogScore\": 9.099999999999998, \"AgeLevel\": [\" 11 to 12\"], \"Statement\": [\"Nice. Your grade is about six\"]}"

So either:

  • you're chaining two dumps. The first dump converts the dictionary into string, but dumping a string is also valid, so the string is dumped, quotes, escaped, etc...
  • you're dumping a string which looks like a dictionary ("{12:1,14:2}").

(If you're not sure of the type, check type(s) before performing the dump)

So when you reload with

par = json.loads(s)

you get a string, not a dictionary, which explains the error message when using [] (since you're trying to pass a key to a string)

Workaround:

use json.loads(json.loads(s)) to recover your data.

and as a better fix, just use dumps once in the serialization process.

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
  • I understand what you have said, but in my code I am using `dumps` only once. Anyway, I thought that if I have string which is formatted like json I should easily convert it to obj using `loads` method – shurrok Oct 31 '17 at 13:49
  • you're using dumps on something that is already a string then. If it's already a string, just write it directly to file, don't use `json` – Jean-François Fabre Oct 31 '17 at 13:51