将多个JSON合并为一个(Python) [英] Merge multiple JSON into single one (Python)
问题描述
我正在寻找一种将多个JSON合并为一个JSON的方法.我的输出是这样的格式:
[{"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"}]
每行都是有效的JSON.我正在寻找一种在Python中完成此操作的方法,例如此网站可以. 此处,您可以找到我用来生成JSON数据的代码. >
我尝试使用jsonmerge
和json-merger
,但是效果不是很好.上面的站点可以完美地完成工作,但是我需要使用Python来完成.
尤其是在jsonmerge
使用文档中的语法的情况下,输出仅前两个值...
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...
...甚至不是JSON的.
如何合并它们?
或类似的带有2D数组输出的
.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
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 ?
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
这篇关于将多个JSON合并为一个(Python)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!