-1

I am searching for a way to merge multiple JSONs into a single one. My output is in this format:

[{"Nome bollettino": "Bollettino 1"}, {"Causale": "1"}, {"Numero": "1"}]
[{"Nome bollettino": "Bollettino 2"}, {"Causale": "2"}, {"Numero": "2"}]
[{"Nome bollettino": "Bollettino 3"}, {"Causale": "3"}, {"Numero": "3"}]
[{"Nome bollettino": "Bollettino 4"}, {"Causale": "4"}, {"Numero": "4"}]
[{"Nome bollettino": "Bollettino 5"}, {"Causale": "5"}, {"Numero": "5"}]
[{"Nome bollettino": "Bollettino 6"}, {"Causale": "6"}, {"Numero": "6"}]
[{"Nome bollettino": "Bollettino 7"}, {"Causale": "7"}, {"Numero": "7"}]
[{"Nome bollettino": "Bollettino 8"}, {"Causale": "8"}, {"Numero": "8"}]
[{"Nome bollettino": "Bollettino 9"}, {"Causale": "9"}, {"Numero": "9"}]
[{"Nome bollettino": "Bollettino 10"}, {"Causale": "10"}, {"Numero": "10"}]

Every single line is a valid JSON. I a looking for a way to do it in Python, like this site do. Here you can find the code I am using to generate JSON data.

I have tried to use jsonmerge and json-merger but it's not so effective. The site above do the work perfect but I need to do it in Python.

Particularly, with jsonmerge using the syntax from the documentation, the output is only the first two values...

result = merge(bollettini, causale, numero)
print(result)

{'Nome bollettino': 'Bollettino 1', 'Causale': '1'}
{'Nome bollettino': 'Bollettino 2', 'Causale': '2'}
{'Nome bollettino': 'Bollettino 3', 'Causale': '3'}

etc...

...which are not even JSON's.

How to merge them ?

lucians
  • 2,239
  • 5
  • 36
  • 64
  • 1
    Why do you not include `"Numero"`? – Willem Van Onsem Oct 27 '17 at 13:51
  • Which format would you like the output JSON to be ? – ZdaR Oct 27 '17 at 13:51
  • @WillemVanOnsem It's the output of "jsonmerge" not mine.. – lucians Oct 27 '17 at 13:52
  • @ZdaR I don't know what do you mean. It should be a valid JSON format. Thanks – lucians Oct 27 '17 at 13:53
  • This is also a valid JSON format: `[{}]`, what are you precisely looking for ? – ZdaR Oct 27 '17 at 13:53
  • @ZdaR Try to validate the second output. The first one it's ok, but I want to made of it a single JSON which contains all the strings. – lucians Oct 27 '17 at 13:55
  • https://stackoverflow.com/questions/38987/how-to-merge-two-dictionaries-in-a-single-expression – garnertb Oct 27 '17 at 13:58
  • Based on your previous question [here](https://stackoverflow.com/questions/46975135/json-wrong-loop-output-in-python), which is obviously related, why don't you create a `list` with all your lines and then call `json.dumps()` on this `list` ? Essentially @NareshChaudhary's answer. – Unatiel Oct 27 '17 at 13:59
  • @Unatiel so, made a list of `json_data` (from related question..) and then dump it ? Just did it now but it's not valid as JSON... – lucians Oct 27 '17 at 14:04
  • @Link I tried too based on said answer, and I cannot reproduce your issue on python 3.6. I took the output of `json.dumps(json_data)` and every single JSON validator I've tried say it outputs a valid RFC4627 JSON. – Unatiel Oct 27 '17 at 14:26

3 Answers3

4

Or like this with a 2D array output.

import json


idx = 0
count = [idx]
data = []
while idx < 10:
  idx += 1

  bollettini = {'Nome bollettino': 'Bollettino ' + str(idx) }
  causale    = {'Causale': str(idx) }
  numero =     {'Numero': str(idx)  }


  data.append([bollettini]+[causale]+[numero])

json_data = json.dumps(data)
print (json_data) #added parenthesis
Marco
  • 1,952
  • 1
  • 17
  • 23
  • The answer is related to [this question](https://stackoverflow.com/questions/46975135/json-wrong-loop-output-in-python). – lucians Oct 27 '17 at 14:28
1

Say you have multiple jsons as list of lists like

l = [[{"Nome bollettino": "Bollettino 1"}, {"Causale": "1"}, {"Numero": "1"}]
[{"Nome bollettino": "Bollettino 2"}, {"Causale": "2"}, {"Numero": "2"}]
[{"Nome bollettino": "Bollettino 3"}, {"Causale": "3"}, {"Numero": "3"}]
[{"Nome bollettino": "Bollettino 4"}, {"Causale": "4"}, {"Numero": "4"}]
[{"Nome bollettino": "Bollettino 5"}, {"Causale": "5"}, {"Numero": "5"}]
[{"Nome bollettino": "Bollettino 6"}, {"Causale": "6"}, {"Numero": "6"}]
[{"Nome bollettino": "Bollettino 7"}, {"Causale": "7"}, {"Numero": "7"}]
[{"Nome bollettino": "Bollettino 8"}, {"Causale": "8"}, {"Numero": "8"}]
[{"Nome bollettino": "Bollettino 9"}, {"Causale": "9"}, {"Numero": "9"}]
[{"Nome bollettino": "Bollettino 10"}, {"Causale": "10"}, {"Numero": "10"}]]

You just need to chain the lists to form a single one! Read more on itertools.chain here

>>> from itertools import chain
>>> list(chain.from_iterable(l))

after dumping your data as json Output:

[{"Nome bollettino": "Bollettino 1"}, {"Causale": "1"}, {"Numero": "1"}, {"Nome bollettino": "Bollettino 2"}, {"Causale": "2"}, {"Numero": "2"}, {"Nome bollettino": "Bollettino 3"}, {"Causale": "3"}, {"Numero": "3"}, {"Nome bollettino": "Bollettino 4"}, {"Causale": "4"}, {"Numero": "4"}, {"Nome bollettino": "Bollettino 5"}, {"Causale": "5"}, {"Numero": "5"}, {"Nome bollettino": "Bollettino 6"}, {"Causale": "6"}, {"Numero": "6"}, {"Nome bollettino": "Bollettino 7"}, {"Causale": "7"}, {"Numero": "7"}, {"Nome bollettino": "Bollettino 8"}, {"Causale": "8"}, {"Numero": "8"}, {"Nome bollettino": "Bollettino 9"}, {"Causale": "9"}, {"Numero": "9"}, {"Nome bollettino": "Bollettino 10"}, {"Causale": "10"}, {"Numero": "10"}]
Keerthana Prabhakaran
  • 3,766
  • 1
  • 13
  • 23
1

... it is quite unclear what you want have, may that helps:

input = [
    [{"Nome bollettino": "Bollettino 1"}, {"Causale": "1"}, {"Numero": "1"}],
    [{"Nome bollettino": "Bollettino 2"}, {"Causale": "2"}, {"Numero": "2"}],
    [{"Nome bollettino": "Bollettino 3"}, {"Causale": "3"}, {"Numero": "3"}],
    [{"Nome bollettino": "Bollettino 4"}, {"Causale": "4"}, {"Numero": "4"}],
    [{"Nome bollettino": "Bollettino 5"}, {"Causale": "5"}, {"Numero": "5"}],
    [{"Nome bollettino": "Bollettino 6"}, {"Causale": "6"}, {"Numero": "6"}],
    [{"Nome bollettino": "Bollettino 7"}, {"Causale": "7"}, {"Numero": "7"}],
    [{"Nome bollettino": "Bollettino 8"}, {"Causale": "8"}, {"Numero": "8"}],
    [{"Nome bollettino": "Bollettino 9"}, {"Causale": "9"}, {"Numero": "9"}],
    [{"Nome bollettino": "Bollettino 10"}, {"Causale": "10"}, {"Numero": "10"}]
]

result = []
for line in input:
    result.append({k:v for x in line for (k,v) in x.items()})

result:

[{'Causale': '1', 'Nome bollettino': 'Bollettino 1', 'Numero': '1'},
 {'Causale': '2', 'Nome bollettino': 'Bollettino 2', 'Numero': '2'},
 {'Causale': '3', 'Nome bollettino': 'Bollettino 3', 'Numero': '3'},
 {'Causale': '4', 'Nome bollettino': 'Bollettino 4', 'Numero': '4'},
 {'Causale': '5', 'Nome bollettino': 'Bollettino 5', 'Numero': '5'},
 {'Causale': '6', 'Nome bollettino': 'Bollettino 6', 'Numero': '6'},
 {'Causale': '7', 'Nome bollettino': 'Bollettino 7', 'Numero': '7'},
 {'Causale': '8', 'Nome bollettino': 'Bollettino 8', 'Numero': '8'},
 {'Causale': '9', 'Nome bollettino': 'Bollettino 9', 'Numero': '9'},
 {'Causale': '10', 'Nome bollettino': 'Bollettino 10', 'Numero': '10'}]
Maurice Meyer
  • 17,279
  • 4
  • 30
  • 47