-2

Want to remove single quote from value before and after the bracket only. i don't want it to string. because of this single quote, this json dictionary becomes invalid.

{
    'NetworkInterfaces': '[{
        "AssociatePublicIpAddress":true,
        "DeleteOnTermination":true,
        "Description":"Primary network interface",
        "DeviceIndex":0,"Groups":["sg-0b60a9f7e75aba4d4"],
        "Ipv6AddressCount":0,
        "Ipv6Addresses":[],
        "NetworkInterfaceId":"eni-0538f01e55dda59e8",
        "PrivateIpAddress":"172.16.18.79",
        "PrivateIpAddresses":[{"Primary":true,"PrivateIpAddress":"172.16.18.79"}],
        "SecondaryPrivateIpAddressCount":0,
        "SubnetId":"subnet-0b2e2d39e5c1a8af9",
        "InterfaceType":"interface"
        }]'
}
Mike Scotty
  • 10,530
  • 5
  • 38
  • 50
  • Suppose u set it to variable `x`, then doing `x['NetworkInterfaces'] = JSON.parse(x.NetworkInterfaces);` should work well and solves your problem – Dhana D. Sep 01 '21 at 08:15
  • You can try to do json.loads(), if the string you pass it which will throw a ValueError – sarath ravi Sep 01 '21 at 08:19
  • 1
    Does this answer your question? [How to parse data in JSON format?](https://stackoverflow.com/questions/7771011/how-to-parse-data-in-json-format) – SuperStormer Sep 01 '21 at 08:31
  • Do you want to convert that to JSON ? Why do you want to remove the quotes ? – Ram Sep 01 '21 at 08:41
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Sep 04 '21 at 08:33

1 Answers1

-1

What you've got there is a dictionary with a JSON value. You can simply reassign the value with the JSON converted to Python objects:

import json, pprint
d = { 'NetworkInterfaces': '[{ "AssociatePublicIpAddress":true, "DeleteOnTermination":true, "Description":"Primary network interface", "DeviceIndex":0,"Groups":["sg-0b60a9f7e75aba4d4"], "Ipv6AddressCount":0, "Ipv6Addresses":[], "NetworkInterfaceId":"eni-0538f01e55dda59e8", "PrivateIpAddress":"172.16.18.79", "PrivateIpAddresses":[{"Primary":true,"PrivateIpAddress":"172.16.18.79"}], "SecondaryPrivateIpAddressCount":0, "SubnetId":"subnet-0b2e2d39e5c1a8af9", "InterfaceType":"interface" }]' }
d['NetworkInterfaces'] = json.loads(d['NetworkInterfaces'])
pprint.pprint(d)

Output:

{'NetworkInterfaces': [{'AssociatePublicIpAddress': True,
                        'DeleteOnTermination': True,
                        'Description': 'Primary network interface',   
                        'DeviceIndex': 0,
                        'Groups': ['sg-0b60a9f7e75aba4d4'],
                        'InterfaceType': 'interface',
                        'Ipv6AddressCount': 0,
                        'Ipv6Addresses': [],
                        'NetworkInterfaceId': 'eni-0538f01e55dda59e8',
                        'PrivateIpAddress': '172.16.18.79',
                        'PrivateIpAddresses': [{'Primary': True,      
                                                'PrivateIpAddress': '172.16.18.79'}],
                        'SecondaryPrivateIpAddressCount': 0,
                        'SubnetId': 'subnet-0b2e2d39e5c1a8af9'}]}
Mike Scotty
  • 10,530
  • 5
  • 38
  • 50