0

I have a list of tuple and want to make JSON from it

[(1, 'a', 'A'), (2, 'b', 'B'), (3, 'c', 'C'), (4, 'd', 'D'), (5, 'e', 'E'), (6, 'f', 'F'), (7, 'g', 'G'), (8, 'h', 'H'), (9, 'i', 'I')]

And Expected JSON

{
    "List": [
                {
                    "name": "1",
                    "description": "a",
                    "type": "A"
                },
                {
                    "name": "2",
                    "description": "b",
                    "type": "B"
                },
                {
                    "name": "3",
                    "description": "c",
                    "type": "C"
                },
               and so on....
            ]
}

How can i do that?

The duplicate identified answers didn't work and gave this error

ValueError: dictionary update sequence element #0 has length 3; 2 is required
tripleee
  • 175,061
  • 34
  • 275
  • 318
prashantitis
  • 1,797
  • 3
  • 23
  • 52
  • Possible duplicate of [more pythonic way to format a JSON string from a list of tuples](http://stackoverflow.com/questions/13761054/more-pythonic-way-to-format-a-json-string-from-a-list-of-tuples) – Serenity Jun 10 '16 at 08:16
  • that didn't work, gave error ValueError: dictionary update sequence element #0 has length 3; 2 is required – prashantitis Jun 10 '16 at 08:18
  • He probably only read your title, which certainly sounds like you want to do exactly what's in the nominated duplicate. I'll edit your title for you. – tripleee Jun 10 '16 at 08:29
  • @tripleee: with actions comes responsibility, if he is active enough to mark it duplicate then he must also do required research on it as well, because as people see DUPLICATE in SO, they start giving negative votes – prashantitis Jun 10 '16 at 08:52
  • I'm afraid the burden of creating a useful title when posting a question is on you. Please understand that removing noise to keep the signal tolerable on this site is a major effort, handled largely by unpaid volunteers. But individual mistakes are usually harmless, especially if you can correct any misunderstandings quickly. – tripleee Jun 10 '16 at 09:22
  • Sorry I didn't mean that. Anyways thanks for your mature words and title. I would keep that thing in my mind for future – prashantitis Jun 10 '16 at 12:10

1 Answers1

2

zip the keys and each tuple calling dict on the result then json.dumps:

import json
keys = ["name", "description","type"]

l=[(1, 'a', 'A'), (2, 'b', 'B'), (3, 'c', 'C'), (4, 'd', 'D'), (5, 'e', 'E'), (6, 'f', 'F'), (7, 'g', 'G'), (8, 'h', 'H'), (9, 'i', 'I')]


js =  json.dumps({"List":[dict(zip(keys, tup)) for tup in l]})

Which gives you:

'{"List": [{"type": "A", "name": 1, "description": "a"}, {"type": "B", "name": 2, "description": "b"}, {"type": "C", "name": 3, "description": "c"}, {"type": "D", "name": 4, "description": "d"}, {"type": "E", "name": 5, "description": "e"}, {"type": "F", "name": 6, "description": "f"}, {"type": "G", "name": 7, "description": "g"}, {"type": "H", "name": 8, "description": "h"}, {"type": "I", "name": 9, "description": "i"}]}'
Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321