Merge multiple JSON into single one (Python)

2024/11/16 16:29:01

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 ?

Answer

Or like this with a 2D array output.

import jsonidx = 0
count = [idx]
data = []
while idx < 10:idx += 1bollettini = {'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
https://en.xdnf.cn/q/119173.html

Related Q&A

I need to filter contents of my text file

I have a text file that I want to loop through, slice some contents, and store in a separate list. The text file contains:blu sre before we start start the process blah blah blah blha end the process b…

How to remove a number from a list that has a range between two numbers? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 6 years ago.Improve…

How do I solve an attribute error?

So like I said before my code (Or my current project that I am working on) is riddled with errors. So far I have at least solved a dozen errors or more and honestly I just give up. I mean God knows how…

Two variables in Django URL

I want a URL something like this:/(category)/(post-slug)On the link this is what I have:{% url blog.category blog.slug %}and for the url.py:url(r^(I DON"T KNOW WHAT TO PUT ON THIS PART TO GET THE …

Python 3.2 Replace all words in a text document that are a certain length?

I need to replace all words in a text document that are of length 4 with a different word. For example, if a text document contained the phrase "I like to eat very hot soup" the words "l…

Python split list at zeros [duplicate]

This question already has answers here:Closed 11 years ago.Possible Duplicate:Remove leading and trailing zeros from multidimensional list in Python if I have a list such as:my_list = [[1,2,0,1], [1,0…

Creating Dynamic Text Using the blit method [duplicate]

This question already has answers here:How to display text with font and color using pygame?(7 answers)Closed last year.Im creating a basic game where a black square moves around the screen. There are…

Regex to match special list items

I have weird list of items and lists like this with | as a delimiters and [[ ]] as a parenthesis. It looks like this:| item1 | item2 | item3 | Ulist1[[ | item4 | item5 | Ulist2[[ | item6 | item7 ]] | i…

How to choose the best model dynamically using python

Here is my code im building 6 models and i am getting accuracy in that, how do i choose that dynamically which accuracy is greater and i want to execute only that model which as highest accuracy."…

How do you access specific elements from the nested lists

I am trying to access elements from the nested lists. For example, file = [[“Name”,”Age”,”Medal”,”Location”],[“Jack”,”31”,”Gold”,”China”],[“Jim”,”29”,”Silver”,”US”]]This data c…