-1

I have a list of ec2 instanceID like below:

['i-111111111111', 'i-22222222222']

And I want to convert it to json.

{
  "instance_id": "i-111111111111",
  "instance_id": "i-222222222222"
}

Can someone please advise how to do it in Python?

khelwood
  • 55,782
  • 14
  • 81
  • 108
nat
  • 15
  • 4
  • 1
    What did you try? Also it does not really make sense to have duplicate keys. – Marcin May 26 '22 at 09:58
  • https://stackoverflow.com/questions/21832701/does-json-syntax-allow-duplicate-keys-in-an-object – jhilmer May 26 '22 at 10:05
  • Thanks for feedback @Marcin @jhilmer Actually I will be using this script for terraform external data source. That is why I'm using a single key(instance_id) because it be used in output section. `output "instance_id_list" { value = data.external.get_instanceid_using_python.result.instance_id }` Do you have better approach on this? Sorry I'm new to python. – nat May 26 '22 at 10:14

1 Answers1

0
li = ['i-111111111111', 'i-22222222222']
di = dict()
for i in li:
   a = {'instance_id':i}
   di.update(a)
print(di)

But having a single key is not a good approach

GeekGroot
  • 102
  • 6
  • Thanks for feedback @GeekGroot Actually I will be using this script for terraform external data source. That is why I'm using a single key(instance_id) because it be used in output section. `output "instance_id_list" { value = data.external.get_instanceid_using_python.result.instance_id }` Do you have better approach on this? Sorry I'm new to python. – nat May 26 '22 at 10:12