How to convert List of JSON frames to JSON frame

2024/10/14 5:13:29

I want to convert List of JSON object ot Single JSON frame

Here is my code

for i in user1:name=i.namepassword=i.passwordid1=i.iduser = { "name" : name,"password" : password,"id":id1}user = json.dumps(user)userid.append(user)

I am getting output is

['{"password": "A123", "name": "Sam", "id": 1}', '{"password":"B123", "name": "Sammy", "id": 2}', '{"password": "C123", "name":"Abhi", "id": 3}']

What I want is something like

{{"password": "A123", "name": "Sam", "id": 1},{"password":"B123", "name": "Sammy", "id": 2},{"password": "C123", "name":"Abhi", "id": 3}}

Answer

json.dumps() returns a string, so you're append strings to userid—and thus end up with the list of strings shown.

If you want a dictionary of dictionaries using the id as the primary key, all you need is:

users = {i.id: {"name": i.name, "password": i.password} for i in user1}
https://en.xdnf.cn/q/117996.html

Related Q&A

Python 2.7 The packaging package is required; normally this is bundled with this package

I expect this has to do with the cryptography module, but Im not sure.Traceback (most recent call last):File "<string>", line 11, in <module>File "c:\python27\lib\site-packag…

Couple the data in all possible combinations

I have data in column in two columns like thisId Value 1 a 2 f 1 c 1 h 2 aand Id like couple the data of the Value column in all possible combinations based on the same Id such as(a,c) (a,h)…

Python - Find date from string

Would anyone know a regex string or another method of obtaining the date and time from this string into variables? The position of the string could change, so line and char no would not work. This is …

Get 1st column values on .csv file on python

i am newbie at python programming, i have a .csv file containing mac address and signal strength data from an AP consider my csv data is:i want to get just mac address values which is the 1st row, ref…

how to click mouse over sub menu in selenium?

I want to click invisible htmls sub menu click.*invisible html source<ul class="options"> <li class="_ranking-attr-filter-container _sub-menu-target"> <span>Hide w…

SSL: CERTIFICATE_VERIFY_FAILED certificate verify failed (_ssl.c.661)

Im trying to install nltk on my Mac, but I keep getting this error message after following these instructions: Install NLTK: sudo pip install -U nltk Install Numpy (optional): sudo pip install -U numpy…

Real-time reading of terminal output from server

Im trying to process images from my camera on my server and get the information after processing on my local machine in real-time. I can get necessary information as terminal outputs on my server, but …

Transform map to mapPartition using pyspark

I am trying to load a tensorflow model from disk and predicting the values.Codedef get_value(row):print("**********************************************")graph = tf.Graph()rowkey = row[0]check…

Module google_auth_httplib2 not found after pip installing google-cloud How can I fix it?

I used pip to install cloud-storage, like this:$ pip install --upgrade google-cloudWhen I started my application, I got an error that said no module named google_auth_httplib2 was found. I used pip lis…

python unbinding/disable key binding after click and resume it later

Im trying to unbind/disable key once its clicked, and resume its function after 2s. But I cant figure out the code for the unbinding. The bind is on window. Heres the code that I tried so far:self.choi…