user3290431
user3290431

Reputation: 409

Convert list to JSON in Python

I have the following in a list:

['allowUsageAnalytics, Yes', 'application, f5app', 'costcenter, f5costcenter', 'customImageId, OPTIONAL', 'declarationUrl, none', 'environment, f5env', 'group, f5group', 'imageName, Good25Mbps', 'instanceType, m3.large', 'managementSubnetAz1, subnet-073d3f3d04195f0c5', 'managementSubnetAz2, subnet-0ccfbd4eee88fcc5c', 'ntpServer, 0.pool.ntp.org', 'owner, f5owner', 'restrictedSrcAddress, 0.0.0.0/0', 'restrictedSrcAddressApp, 0.0.0.0/0', 'sshKey, chzhang', 'subnet1Az1, subnet-0cc1233b3ffeab89b', 'subnet1Az2, subnet-065b60bb848a3530a', 'timezone, UTC', 'Vpc, vpc-0fc1a35688397512f']

I need to convert it to the below JSON:

[
  {
    "ParameterKey": "allowUsageAnalytics",
    "ParameterValue": "Yes"
  },
  {
    "ParameterKey": "application",
    "ParameterValue": "f5app"
  }
]

This is the code:

print(json.dumps([{'ParameterKey': x, 'ParameterValue': y} for x, y in content], indent=4))

But I am getting the following error:

File "/Users/a/PycharmProjects/BIGIP-DevOps/CFT_print.py", line 22, in <module>
    print(json.dumps([{'ParameterKey': x, 'ParameterValue': y} for x, y in content], indent=4))
  File "/Users/a/PycharmProjects/BIGIP-DevOps/CFT_print.py", line 22, in <listcomp>
    print(json.dumps([{'ParameterKey': x, 'ParameterValue': y} for x, y in content], indent=4))
ValueError: too many values to unpack (expected 2)

Any ideas?

Upvotes: 0

Views: 683

Answers (2)

pault
pault

Reputation: 43504

You can split each element in your list on ", " and then use the dict constructor.

import json
print(json.dumps(dict(map(lambda x: x.split(", "), content)), indent=4))

Which results in:

{
    "group": "f5group", 
    "restrictedSrcAddress": "0.0.0.0/0", 
    "declarationUrl": "none", 
    "managementSubnetAz2": "subnet-0ccfbd4eee88fcc5c", 
    "subnet1Az1": "subnet-0cc1233b3ffeab89b", 
    "ntpServer": "0.pool.ntp.org", 
    "managementSubnetAz1": "subnet-073d3f3d04195f0c5", 
    "sshKey": "chzhang", 
    "subnet1Az2": "subnet-065b60bb848a3530a", 
    "environment": "f5env", 
    "application": "f5app", 
    "customImageId": "OPTIONAL", 
    "imageName": "Good25Mbps", 
    "restrictedSrcAddressApp": "0.0.0.0/0", 
    "costcenter": "f5costcenter", 
    "owner": "f5owner", 
    "timezone": "UTC", 
    "Vpc": "vpc-0fc1a35688397512f", 
    "instanceType": "m3.large", 
    "allowUsageAnalytics": "Yes"
}

As @faisal pointed out, to get your desired output you can extend this to:

print(
    json.dumps(
        [
            {'ParametersKey':x, 'ParameterValue':y} 
            for x,y in dict(map(lambda x: x.split(', '), content)).items()
        ], 
        indent=4
    )
)
#[
#    {
#        "ParameterValue": " f5group", 
#        "ParametersKey": "group"
#    }, 
#    {
#        "ParameterValue": " 0.0.0.0/0", 
#        "ParametersKey": "restrictedSrcAddress"
# ... and so on

Upvotes: 1

Prune
Prune

Reputation: 77837

content is a list of many elements. for x, y in content is not a valid iterator.

You need to nest the expressions:

for elem in content

to iterate through the strings,

for x, y in elem.split(", ")

to get the two words in each string.

Can you take it from there?

Upvotes: 0

Related Questions