10

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.

5
  • What is this line for: mystring = json.dumps(mystring)? Besides your string is not valid JSON (single quotes instead of double). Commented May 14, 2019 at 0:47
  • 1
    Where did mystring come from? It looks like you took a perfectly good dict and converted it into a string. The correct course of action is most likely not to add more processing steps, but to remove an earlier, incorrect step. Commented May 14, 2019 at 0:47
  • If you're really stuck with this string, you may have to use something like ast.literal_eval or a permissive JSON parser. json.dumps will definitely not be part of the correct solution. Commented May 14, 2019 at 0:49
  • mystring is not valid JSON to begin with — it needs double quotes to be json. Commented May 14, 2019 at 0:49
  • 2
    You serialized your Python str object into a JSON string object. So when you deserialize it, it is going to be a string. As others have noted, your string doesn't actually contain valid JSON to begin with, but in any case, you almost certainly didn't mean to json.dumps(mystring) first. That doesn't make any sense. Commented May 14, 2019 at 0:50

3 Answers 3

12

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

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

-1

Yes json decoder likes double quotes for keys and values, and I think you can use python to do the replacement, try if applies:

mystring = "{'Date': 'Fri, 19 Apr 2019 03:58:04 GMT', 'Server': 'Apache/2.4.39', 'Accept-Ranges': 'bytes'}"
json_string = mystring.replace("'", "\"")
d = json.loads(json_string)

dstring = json.dumps(d)
myJson = json.loads(dstring)

print(str(myJson.keys()))

6 Comments

Are you sure the mystring.replace will replace only the key/value commas? Not any other commas inside the value?
the 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
Yes 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.
ohh I see your concern... ast.literal_eval is a more practical solution in this particular case
I 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?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.