P
P
pavel0staratel2015-05-06 14:40:02
Python
pavel0staratel, 2015-05-06 14:40:02

How to parse a string with not quite correct json?

There is a string that looks like invalid JSON, where the data is mostly represented as a key:value, but sometimes there is only a key, or 2 keys and one value.

input_str="""name1: value1; name2: value2; name3; prefix: name4: value4;"""

I want to get a dictionary like this:
output_dict={'name1': 'value1', 'name2': 'value2', 'name3':True, 'prefix name4': 'value4'}

I tried to parse it using json.loads (previously placing quotes, etc.), but I don’t know how to handle situations with name3 and name4.
UPDATE:
Did this:
import json

input_str="""name1: value1; name2: value2; name3; prefix: name4: value4;"""
if input_str[-1]==';':
    input_str=input_str[:-1]

god_str='","'.join([{0: item+': True', 1: item}.get(item.count(':'),item.replace(":", " ", 1)) for item in json.dumps(input_str).split('; ')])
json_str='{%s}'%god_str.replace(': ','":"')

output_dict=json.loads(json_str)
print(output_dict)

Result:
{'prefix  name4': 'value4', 'name1': 'value1', 'name2': 'value2', 'name3': 'True'}

Everything is fine, that's just True in quotes.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Andrey Dugin, 2015-05-06
@pavel0staratel

def tokenize(data):
    cleanup = lambda entry: entry.replace(':', '').strip()
    for entry in data.strip(';').split(';'):
        entry = map(cleanup, entry.rsplit(':',1))
        if len(entry) == 1:
            entry.append(True)
        yield entry

input = 'name1: value1; name2: value2; name3; prefix: name4: value4;'
print dict(tokenize(input))

Result:
You can also play around like this:
>>> dict(re.findall('\s*([\w\s:]+?)\s*(?::\s*([\w\s]*)\s*)?(?=[;$])', input))
{'prefix: name4': 'value4', 'name2': 'value2', 'name3': '', 'name1': 'value1'}

D
Denis Ineshin, 2015-05-06
@IonDen

You can do this:
jsfiddle.net/IonDen/gm5yvmrj

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question