Normalise JSON with Python

2024/9/20 7:48:41

Prompt me, please, how to normalize this JSON file using Python?

This question is related to the previous

The current JSON contains:

{"total_stats": [{"domain": "domain.com","uptime": "100"},{"domain": "domain.com","threats": "345.01111783804436"}]
}

Desirable

{"total_stats": [{"domain": "domain.com","uptime": "100","threats": "345.01111783804436"}]
}
Answer

If you want to merge the dictionaries according the "domain" key you can use (note: if dictionaries have common keys, the last dictionary value will be used):

dct = {"total_stats": [{"domain": "domain.com", "uptime": "100"},{"domain": "domain.com", "threats": "345.01111783804436"},]
}out = {}
for d in dct["total_stats"]:out.setdefault(d["domain"], {}).update(d)dct["total_stats"] = list(out.values())
print(dct)

Prints:

{"total_stats": [{"domain": "domain.com","uptime": "100","threats": "345.01111783804436",}]
}
https://en.xdnf.cn/q/119768.html

Related Q&A

How to change decimal separator?

I have an Excel spreadsheet (an extract from SAP). I turn this into a DataFrame, do calculations and save it to an SQLite database. The Excel spreadsheet has comma as decimal separator. The SQLite data…

Unable to scrape the name from the inner page of each result using requests

Ive created a script in python making use of post http requests to get the search results from a webpage. To populate the results, it is necessary to click on the fields sequentially shown here. Now a …

Python Integer and String Using [duplicate]

This question already has an answer here:How can I concatenate str and int objects?(1 answer)Closed 7 years ago.for size in [1, 2, 3, 4]:result = 0print("size=" + str(size))for element in ra…

Beginner to python: Lists, Tuples, Dictionaries, Sets [duplicate]

This question already has an answer here:What is the difference between lists,tuples,sets and dictionaries? [closed](1 answer)Closed 3 years ago.I have been trying to understand what the difference is…

TypeError: NoneType object is not iterable in Python in csv

I am new to python, and trying to create a program which opens a csv file. The user is supposed to enter a barcode , then the program finds that product and the cost of the product. However I got an er…

No such Element Exception using selenium in python

from selenium import webdriver from selenium.webdriver.common.keys import Keys chrome_path=r"C:\Users\Priyanshu\Downloads\Compressed\chromedriver_win32\chromedriver.exe" driver=webdriver.Chro…

Web scraping, cant get the href of a tag

Im trying to scrape this Page https://rarity.tools/thecryptodads Using Selenium in python. At the top of the right of each card below, theres the owner name that contains a link once pressed, it takes …

Using Python Pandas to fill new table with NaN values

Ive imported data from a csv file which has columns NAME, ADDRESS, PHONE_NUMBER. Sometimes, at least 1 of the columns has a missing value for that row. e.g0 - Bill, Flat 2, 555123 1 - Katie, NaN, NaN 2…

sympy AttributeError: Pow object has no attribute sin

I have read this SO post which says namespace conflict is one reason for this error. I am falling to this error frequently. So, Id like to learn what exactly is happening here? What is expected by the…

Tkinter unbinding key event issue

In the code below, pressing the space bar twice results in two successive beeps. I want to avoid this and instead disable the key while the first beep is happening. I thought unbinding the space key mi…