Home > Mobile >  convert list with keys & values to json format( python)
convert list with keys & values to json format( python)

Time:02-01

code i am using

show_config = self.show_task(ha_task) show_config = show_ha_config["tasks"][0]["task-details"][0][ "output" ]

    # need to decode into base64
    show_decoded = base64.b64decode(
        bytes(show_config, "utf-8")
    ).decode("ascii")
    show_conf = show_decoded.split("\n")
    return show_conf
return show_conf = [
    '',
    'VSID:            0   ',
    'VRID:            0   ',
    'Type:            VSX Gateway',
    'Name:            chckpt-fw1a',
    'Security Policy: VS-policy',
    'Installed at:    12Jan2023 21:57:15',
    'SIC Status:      Trust',
    'Connections number: 52',
    'Connections peak: 152',
    'Connections limit:  14900',
]

i am using below code to convert it into json but response is not good

json_format = json.dumps(show_conf)
return json_format

json_format i get:

["", "VSID:            0   ", '
                                  '"VRID:            0   ", "Type:            '
                                  'VSX Gateway", "Name:            '
                                      'chckpt-fw1a", "Security Policy: '
                                      'VS-policy", "Installed at:    '
                                      '12Jan2023 21:57:15", "SIC Status:      '
                                      'Trust", "Connections number: 52", '
                                      '"Connections peak:   152", "Connections '
                                      'limit:  14900",]

json_format i need:

['', 
'"VSID": "0"   ',
'"VRID": "0"   ',
'Type:   "VSX Gateway"',
'"Name": "chckpt-fw1a"',
'"Security Policy": "VS-policy"',
"Installed at":    "12Jan2023 21:57:15"',
"SIC Status":      "Trust"',
"Connections number": "52"',
"Connections peak":   "152"',
"Connections limit":  "14900"',]

CodePudding user response:

You probably first want to convert this list to dictionary. To do this, we first need to parse each item in it and then dump it all to a string / file.

import json  # Python built-in json parser

# Raw data
output = [
    '',
    'VSID:            0   ',
    'VRID:            0   ',
    'Type:            VSX Gateway',
    'Name:            chckpt-fw1a',
    'Security Policy: VS-policy',
    'Installed at:    12Jan2023 21:57:15',
    'SIC Status:      Trust',
    'Connections number: 52',
    'Connections peak: 152',
    'Connections limit:  14900',
]

# Declaring the dictionary for output
output_dict = {}

# Iterating through each item of `output` list
for item in output:
    # Lets take this item as sample - 'VSID:            0   '

    # Split the string by `: ` separator to list. Separator WILL be removed.
    # Result will look like this -
    #     ['VSID', '           0   ']
    splitted = item.split(": ")

    # Skip empty items or single keys
    if len(splitted) <= 1:
        continue

    output_dict[
        splitted[0].strip()  # Key - 'VSID'
    ] = splitted[1].strip()  # Value - '0'
    # `.strip()` will remove all the trailing and leading whitespaces

# Dump the generated dictionary to this variable as string.
# `indent=4` will generate the output in more human-readable format.
jsn = json.dumps(output_dict, indent=4)

# Display the json
print(jsn)
  • Related