Python Forum
convert unlabeled list of tuples to json (string)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
convert unlabeled list of tuples to json (string)
#1
Question 
Say i have a list of unlabeled tuples which i want to convert to JSON
Where each tuple is an object (of the same type) which represents a user
the first value of the tuple represents a 'username' and second a 'role'
Sample Input:
[('Andy', '001'), ('Off0', '010'), ('Pla0', '100'), ('Pla1', '100'), ('Pla2', '100'), ('Pla3', '100')]
Output:
Output:
[ { "username": "Andy", "role": "001" }, { "username": "Off0", "role": "010" }, { "username": "Pla0", "role": "100" }, { "username": "Pla1", "role": "100" }, { "username": "Pla2", "role": "100" }, { "username": "Pla3", "role": "100" }, ]
Note: i do not care about the white spaces

i can convert one tuple with:
json.dumps({'username':list[0][0], 'role':list[0][1]})
but i do not understand to conbine all the tuples of the list together

Thanks!

P.S. i am python newbie, but i am familiar with the basic concepts (like syntax)
Reply
#2
import json
users = [('Andy', '001'), ('Off0', '010'), ('Pla0', '100'), ('Pla1', '100'), ('Pla2', '100'), ('Pla3', '100')]
keys = ['username', 'role']

data = [dict(zip(keys, user)) for user in users]
json_data = json.dumps(data, indent=4)
print(json_data)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
(Apr-26-2021, 06:42 PM)buran Wrote:
import json
users = [('Andy', '001'), ('Off0', '010'), ('Pla0', '100'), ('Pla1', '100'), ('Pla2', '100'), ('Pla3', '100')]
keys = ['username', 'role']

data = [dict(zip(keys, user)) for user in users]
json_data = json.dumps(data, indent=4)
print(json_data)



Thanks man!

Explaining what you did for anyone in the future who has this problem
data = [dict(zip(keys, user)) for user in users]
converts the (unlabeled) tuple to a dictionary (effectivly a named tuple)
and then
json_data = json.dumps(data, indent=4)
simply encode it
Reply
#4
(Apr-26-2021, 06:59 PM)masterAndreas Wrote: dictionary (effectivly a named tuple)
dict is not namedtuple! These are two very different container types

If you want to use namedtuple

import json
from collections import namedtuple
users = [('Andy', '001'), ('Off0', '010'), ('Pla0', '100'), ('Pla1', '100'), ('Pla2', '100'), ('Pla3', '100')]
User = namedtuple('User', 'username role')
print(User('Andy', '001'))
data = [User(*item)._asdict() for item in users]
json_data = json.dumps(data, indent=4)
print(json_data)
In my opinion simply using a dict is better.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
Quote:dictionary (effectively a named tuple)

would it be more accurate if I rephrased as
Quote:dictionary (effectively for our purposes, a pseudo named-tuple)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python Script to convert Json to CSV file chvsnarayana 8 2,346 Apr-26-2023, 10:31 PM
Last Post: DeaD_EyE
  [split] Parse Nested JSON String in Python mmm07 4 1,428 Mar-28-2023, 06:07 PM
Last Post: snippsat
  Response.json list indices must be integers or slices, not str [SOLVED] AlphaInc 4 6,191 Mar-24-2023, 08:34 AM
Last Post: fullytotal
  convert string to float in list jacklee26 6 1,820 Feb-13-2023, 01:14 AM
Last Post: jacklee26
  Adding values with reduce() function from the list of tuples kinimod 10 2,518 Jan-24-2023, 08:22 AM
Last Post: perfringo
  convert a list to links Pir8Radio 3 1,043 Nov-28-2022, 01:52 PM
Last Post: Pir8Radio
  convert this List Comprehensions to loop jacklee26 8 1,418 Oct-21-2022, 04:25 PM
Last Post: deanhystad
  how to convert tuple value into string mg24 2 2,237 Oct-06-2022, 08:13 AM
Last Post: DeaD_EyE
  Convert Json to table format python_student 2 5,073 Sep-28-2022, 12:48 PM
Last Post: python_student
  Convert string to float problem vasik006 8 3,270 Jun-03-2022, 06:41 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020