Tuples conversion into JSON with python [closed]

2024/11/7 22:33:41

I would like some help converting a tuple in format [(X, Y, Z),(..) of type (string, string, int)] to a JSON file in the format:

{"name": "X","children": [{"name": "Y","value": Z}]
}

I have at least 1M values to convert and at the moment I have attempted using a key for the dictionary:

b = (dict(zip(keys,row)) for row in tuples)  

using the JSON library

print (json.dumps(list(b)))  

however this yields a JSON in the format

[{"type": "y", "name": "z", "count": z},...  

Preferably I would like the Y and Z values to be nested under children and the X value to be used once per unique string.

   {"name": "X","children": [{"name": "Y","value": Z},{"name": "Y2","value": Z2}]}
Answer

Assuming you want a list of dicts as the output of the json with each dict be the form in your question:

The following one liner will put it into the data structure your looking for with each tuple generating it's own complete structure:

[{'name':i[0], 'children': [{'name': i[1], 'value': i[2]}]}  for i in tuples]

But I suspect you want the outer name to be unique with inner children being built from multiple tuples, such that there is only one such structure generated with 'name' == X.

To do it in a memory efficient manner start by sorting your tuples by X:

# should work as long as you aren't doing any fancy sorting
stuples = sorted(tuples) name = None
dat = None
for t in stuples:if name != t[0]:if dat is not None:writeDat(json.dumps(dat))name = t[0]dat = {'name': t[0], 'children': [{'name': t[1], 'value': t[2]}]}else:dat['children'].append({'name': t1, 'value': t[2]})

I assume you have a function to write one of these out such as writeDat() that takes the json so you aren't storing them all in ram.

https://en.xdnf.cn/q/120687.html

Related Q&A

Python continue with while [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.This question was caused by a typo or a problem that can no longer be reproduced. While similar q…

Create MySQL database with python [closed]

Closed. This question needs debugging details. It is not currently accepting answers.Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to repro…

Basic Python repetition exercise

I was trying to learn Python when I came upon this question. I am not asking for you to answer the question for me, I just need some help. Note: I am only allowed to use loops and if statements etc. No…

Python 3.3 socket programming error [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.Questions asking for code must demonstrate a minimal understanding of the problem being solved. Incl…

ValueError: math domain error

I wrote this codedef partE():e = 3 * 10 // 3 + 10 % 3print("e).", e)partE()and python comes back with this error message when I try to run it. I do not understand why. Can someone please expl…

how to access objects in python with for loop in different files

This is my file1.json: {"count": 1,"next": null,"previous": null,"results": [{"id": 5883,"url": "https://some.api.com/api/ipam/ip-addres…

multiplicative digital root of a number using loops

I need to find the multiplicative digital root of a number in python using only loops.something that does the same as the below code but using loops:print("multiplicative digital root of a number …

How to access key values in a json files dictionaries with python

I have a script that pulls json data from an api, and I want it to then after pulling said data, decode and pick which tags to store into a db. Right now I just need to get the script to return specifi…

How to use sin(x) and cos(x) functions with eval

I need a program which can make graphs by matplotlib with functions I write in the console. But it doesnt work with trigonometric functions. The code I already wrote is:from numpy import linspace impo…

python - whats the difference between = and ==? [duplicate]

This question already has answers here:What do the symbols "=" and "==" mean in python? When is each used?(5 answers)Closed 5 years ago.I wonder know whats the difference between …