4

How can I write a function in python that that will take a string with multiple dictionaries, one per line, and convert it so that json.loads can parse the entire string in single execution.

For example, if the input is (one dictionary per line):

Input = """{"a":[1,2,3], "b":[4,5]}
           {"z":[-1,-2], "x":-3}"""

This will not parse with json.loads(Input). I need to write a function to modify it so that it does parse properly. I am thinking if the function could change it to something like this, json will be able to parse it, but am not sure how to implement this.:

Input2 = """{ "Dict1" : {"a":[1,2,3], "b":[4,5]},
               "Dict2" : {"z":[-1,-2], "x":-3} }"""
nasia jaffri
  • 803
  • 2
  • 12
  • 21
  • possible duplicate of [Loading & Parsing JSON file in python](http://stackoverflow.com/questions/12451431/loading-parsing-json-file-in-python) – Martijn Pieters Nov 17 '13 at 23:32
  • 1
    Parse the string line by line; if not from a file, then use `str.splitlines()` to split this out into a list of lines. – Martijn Pieters Nov 17 '13 at 23:33
  • I did not find what I was looking for in the post you mentioned. I have to change input to input2 and then load it to json. Splitlines is not creating Dictionary keys as Dict1, or Dict2. – nasia jaffri Nov 17 '13 at 23:40
  • No, you are going about this the wrong way. `Input2` is not valid JSON and all you want is a list of all the objects. Each line us *one* JSON object; parse them as such and build the list as you do so. – Martijn Pieters Nov 18 '13 at 00:32
  • Basically the main object is get input2 from input. – nasia jaffri Nov 18 '13 at 01:09
  • You basically want a list of objects parsed. I am giving you that list. – Martijn Pieters Nov 18 '13 at 01:12
  • If I do str.splitlines(Input), it returns a list, but not a dictionary with keys, such as Dict1, Dict2. – nasia jaffri Nov 18 '13 at 01:13
  • I meant `input.splitlines()`, but yes, now you have a list of JSON strings. Decode each one as In the linked post. – Martijn Pieters Nov 18 '13 at 01:25

1 Answers1

6
>>> import json  
>>>
>>> dict_str = """{"a":[1,2,3], "b":[4,5]}
>>>               {"z":[-1,-2], "x":-3}"""
>>>
>>> #strip the whitespace away while making list from the lines in dict_str 
>>> dict_list = [d.strip() for d in dict_str.splitlines()]
>>>                                                        
>>> dict_list
>>> ['{"a":[1,2,3], "b":[4,5]}', '{"z":[-1,-2], "x":-3}']
>>>
>>> j = [json.loads(i) for i in dict_list]
>>> j
>>> [{u'a': [1, 2, 3], u'b': [4, 5]}, {u'x': -3, u'z': [-1, -2]}]

Not in function form like you requested, but the code would be almost the same. Also, this produces the dicts in a list.

Adding the following might be of use to you

>>> d = {('Dict'+str(i+1)):v for i in range(len(j)) for v in j}
>>> d
>>> {'Dict1': {u'x': -3, u'z': [-1, -2]}, 'Dict2': {u'x': -3, u'z': [-1, -2]}}
Totem
  • 7,189
  • 5
  • 39
  • 66
  • Thanks for your input, but what I am looking for is that each line should be treated as one dictionary. so, "Dict1":{"a":[1,2,3], "b":[4,5]} (the whole thing on the first line) then Dict2 should be the whole thing on 2nd line. – nasia jaffri Nov 18 '13 at 02:55
  • I added something to my post. I am not sure if this is what you meant, but it seems like a step in the right direction perhaps? Did you mean you want a dictionary where the values are the dictionaries from each line of the string? – Totem Nov 18 '13 at 03:05
  • I apologize for such a late response. Yes, I want everything in 1st line in Dict1, everything on 2nd line in Dict2, and so on. I see what you have added in your post, and it is doing what I want, but I am not very good at Python and do not understand clearly what this code is exactly doing. – nasia jaffri Nov 21 '13 at 02:56
  • Glad I could help. In the first block of code, I assign lists to both 'dict_list' and 'j' using 'list comprehensions'. In case you don't know what list comprehensions are, I advise you Google 'python list comprehensions' immediately to find out about one of pythons handiest features. In the second block, I assign a dictionary to 'd' by using a 'dictionary comprehension'. You should Google this after you have grasped list comprehensions. The concepts involved shouldn't take long to understand, and as you can see, they are very useful(and satisfying). – Totem Nov 25 '13 at 02:45