1

I'm using PYTHON :

I'm trying to convert the following array of strings:

tags_list = ["one", "two", "three"]

to an array of JSON array of objects with a specific key as follows:

    "tags": [
        {
            "name": "one"
        },
        {
            "name": "two"
        },
        {
            "name": "three"
        }
    ]

I found several answers for JavaScript not python

Can any body help please

Thanks in-advance

Mohamed Abbase
  • 111
  • 3
  • 15

2 Answers2

3

Almost the same as @sudden_appearance mentioned, but I'd add :

print(json.dumps(json_dict, indent=2)) 

when you log it to the console, for a better format :)

2

try this is full example of your code

import json
tags_list = ["one", "two", "three"]

#prepare array to object
json_dict = {"tags": [{"name": value} for value in tags_list]}

#object to json String
json_string= json.dumps(json_dict)
print(json_string) #for print json if needed
RajP7Jowa
  • 21
  • 4