Reputation: 2329
I have a string that contains a dictionary, and inside there's a field that has values that are list of tuples. When I try to load the string into json, it fails.
The format looks like
{'info_scetion': {'category1': [('response 1', '{"tag1":"str1", "tag2":"str2"}')]}}
This is one of the special inputs I receive, and it is the only one that contains a list of tuple. I do not generate the input, so I cannot change the format. Because JSON cannot parse this string directly, I was thinking about trying to identify the tuple inside the string and pick it out. For the rest, the code should be able to process.
The problem is, I'm not sure how to do it. I tried forming a regex that uses (
and )
in some forms like (.*?)
to get the first incidence, but I cannot guarantee there wouldn't be any )
in the actual tuple.
If I go with this direction, how do I correctly identify the tuple?
If there's another way to do it, what is it?
EDIT: adding the }
at the end
Upvotes: 0
Views: 1802
Reputation: 57105
You 'JSON' is not really a JSON: it is a Python data structure, so parse it as such with the AST module:
s = "{'info_scetion': {'category1': [('response 1', '{\"tag1\":\"str1\", \"tag2\":\"str2\"}')]}}"
result = ast.literal_eval(s)
result
#{'info_scetion': {'category1': \
# [('response 1', '{"tag1":"str1", "tag2":"str2"}')]}}
Once it is imported into Python, you can manipulate it in any way you like. For example, you can "flatten" the list of tuple:
result['info_scetion']['category1'] = list(result['info_scetion']['category1'][0])
#{'info_scetion': {'category1': ['response 1', '{"tag1":"str1", "tag2":"str2"}']}}
Upvotes: 3
Reputation: 3508
Your json is malformed, it is missing a }
at the end.
I tested things with this code and things seem to be fine.
data = {'info_scetion': {'category1': [('response 1', '{"tag1":"str1", "tag2":"str2"}')]}}
print data['info_scetion']['category1'][0][0]
# output >> response 1
print json.loads(data['info_scetion']['category1'][0][1])['tag1']
# output >> str1
Upvotes: -1