2

I have string containing multiple jsons. I want to convert that string to single json object.

For example,

Assuming the following input,

input = """
{
 "a" : {
        "x":"y",
        "w":"z" 
    }
} 

{
"b" : {
       "v":"w",
       "z":"l"
   }
}
"""

The expected output will be:
Output:

{
"a" : {
       "x":"y",
       "w":"z"
   }

"b" : {
       "v":"w",
       "z":"l"
    }
}
CypherX
  • 7,019
  • 3
  • 25
  • 37
M. Twarog
  • 2,418
  • 3
  • 21
  • 39
  • The question doesn't appear to include any attempt at all to solve the problem. Please edit the question to show what you've tried, and show a specific roadblock you're running into with [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve). For more information, please see [How to Ask](https://stackoverflow.com/help/how-to-ask). – Andreas Oct 16 '19 at 06:12
  • Your input seems to be malformed. Is that intentional? – norok2 Oct 16 '19 at 06:24
  • 1
    Your expected output isn't a valid json – sheldonzy Oct 16 '19 at 06:25
  • Some more considerations about dictionary merging: https://stackoverflow.com/a/26853961/4373898 – AlexisBRENON Oct 18 '19 at 16:02

3 Answers3

3

if we treat them as dictionaries and have

>>> a = {'a':{'a':1}}
>>> b = {'b':{'b':1}}

we can simply

>>> a.update(b)
>>> a

{'a': {'a': 1}, 'b': {'b': 1}}
Saswath Mishra
  • 309
  • 2
  • 10
2

you can take advantage of the fact that you can see when a dictionary begins by looking if a line starts with '{':

import json

input = """
{

 "a" : {

        "x":"y",

        "w":"z"

    }

}

{
"b" : {

       "v":"w",

       "z":"l"

   }

}"""

my_dicts = {}
start_dict = False
one_json = ''

for line in input.split('\n'):
    if line.startswith('{'):
        # check if we pass a json
        if start_dict:
            my_dicts.update(json.loads(one_json))
            one_json = ''
        else:
            start_dict = True

    one_json = f'{one_json}\n{line}'

# take the last json
my_dicts.update(json.loads(one_json))

print(my_dicts)

output:

{'a': {'w': 'z', 'x': 'y'}, 'b': {'v': 'w', 'z': 'l'}}
kederrac
  • 16,819
  • 6
  • 32
  • 55
1

Build up a list of dictionaries parsing each character. One could also parse each line.

There is good probability of finding a user library that already does this function but here is a way to go

import json

braces = []
dicts = []
dict_chars = []

for line in inp: # input is a builtin so renamed from input to inp
  char = line.strip()
  dict_chars.append(line)
  if '{' == char:
    braces.append('}')
  elif '}' == char:
    braces.pop()
  elif len(braces) == 0 and dict_chars:
    text = ''.join(dict_chars)
    if text.strip():
      dicts.append(json.loads(text))
    dict_chars = []

Then, merge dictionaries in the list.

merged_dict = {}
for dct in dicts:
  merged_dict.update(dct)
> print(merged_dict)
{u'a': {u'x': u'y', u'w': u'z'}, u'b': {u'z': u'l', u'v': u'w'}}

Output merged dictionary as json string with indentation.

merged_output = json.dumps(merged_dict, indent=4)
Oluwafemi Sule
  • 36,144
  • 1
  • 56
  • 81