python's json: AttributeError: 'str' object has no attribute 'keys'
Solution 1
Rather than dealing with the single quoted string and struggling to convert it into json, just use ast package to convert it into a valid dict.
import ast
mystring = "{'Date': 'Fri, 19 Apr 2019 03:58:04 GMT', 'Server': 'Apache/2.4.39', 'Accept-Ranges': 'bytes'}"
my_dict = ast.literal_eval(mystring)
the result is:
> print(my_dict["Date"])
Fri, 19 Apr 2019 03:58:04 GMT
Solution 2
This code stores the string as a dictionary in a variable called "Tempvar" From that variable you can just use the keys like a regular dictionary.
import json
mystring = "{'Date': 'Fri, 19 Apr 2019 03:58:04 GMT', 'Server': 'Apache/2.4.39', 'Accept-Ranges': 'bytes'}"
exec("tempvar = " + mystring)
mystring = json.dumps(mystring)
myJson = json.loads(mystring)
print(str(tempvar['Date']))
print(str(myJson))
Hope this helps
user9371654
Updated on May 14, 2020Comments
-
user9371654 almost 4 years
I am trying to load a string (the actual program read this line from a file and it is a very large file that I can not manually modify) formatted as a dictionary.
I need to convert the string line into a json object so I can check value of specific key, e.g.
myJson[Date]
.This is the script:
import json mystring = "{'Date': 'Fri, 19 Apr 2019 03:58:04 GMT', 'Server': 'Apache/2.4.39', 'Accept-Ranges': 'bytes'}" mystring = json.dumps(mystring) myJson = json.loads(mystring) print(str(myJson.keys())) print(str(myJson))
I am getting this error:
AttributeError: 'str' object has no attribute 'keys'
I suspect that the
mystring
format is not conforming and that the single quotes should be double quotes? Given that I have a large data, and I can not simply replace single colons with double one using simple search/replace as single colons may be included in the values which I should not modify. If this is the cause of the problem, is there any way to replace the colons of the key/value pair only without touching the colons in the values? I am hoping that this is not the problem. -
user9371654 almost 5 yearsAre you sure the
mystring.replace
will replace only the key/value commas? Not any other commas inside the value? -
Ricardo Wong almost 5 yearsthe replace command replaces single quotes with double quotes, not the commas. You can print the result of the replace command only, to see how it works
-
user9371654 almost 5 yearsYes I mean quotes. that was a typo. But as I said, the data in the values may contain double quotes. This is dangerous to blindly replace quotes. Unless there isa method that can guarantee that only key/value quotes get replaces.
-
Ricardo Wong almost 5 yearsohh I see your concern... ast.literal_eval is a more practical solution in this particular case
-
user9371654 almost 5 yearsI mean inside the value, it is possible that I have single quotes that should remain. Example:
'Date': 'Fri, '19' Apr 2019 03:58:04 GMT'
. I should not change the quotes in 19. Or possibly there will be double quotes like Example:'Date': 'Fri, "19" Apr 2019 03:58:04 GMT'
which I do not know if will cause problems if I converted the single quotes to double quotes? -
Ricardo Wong almost 5 yearsThe problem with that string is that will be interpreted as two different values with something in the middle like 'Fri, ' 19 and ' Apr 2019... '. The other case is easy to solve with an extra par of replacements