dictionary value is dict but printing as string in json dump

I have a script that is working fine except for this tiny issue. My script is looping over list items and appending a json string over a loop and then doing json dump to file.

My json string:

main_json = {"customer": {"main_address": "","billing_address": "","invoice_reference": "","product": []}}

main loop:

   for row in result:
        account_id = ACCOUNTID_DATA_CACHE.get(row['customer.main_address.customer_id'])
        if account_id is None or account_id != row['customer.main_address.customer_id']:
            if main_json:
                results.append(main_json)
            main_json = {"customer": {"main_address": "","billing_address": "","invoice_reference": "","product": []}}
            main_address = {}
            billing_address = {}          
            for key,value in row.items():
                if key.startswith('customer.main_address'):
                    main_address[key.split(".")[2]] = value
                if key.startswith('customer.billing_address'):
                    billing_address[key.split(".")[2]] = value

            billing_address_copy = billing_address.copy()

            for mkey,mvalue in main_address.items():
                for bkey,bvalue in billing_address_copy.items():
                    if str(bvalue) == str(mvalue):
                        bvalue = ''
                        billing_address_copy[bkey] = bvalue


            if all(value == '' for value in billing_address_copy.values()) is True:
                main_json['customer']['billing_address'] = ''
            else:
                main_json['customer']['billing_address'] = billing_address
            main_json['customer']['main_address'] = main_address
            product = parse_products(row)
            main_json['customer']['product'].append(product)

...

def parse_products(row):
product = {}
x = {}
for key,value in row.items():
    if key.startswith('customer.product'):
        product[key.split(".")[2]] = value
    if key.startswith('customer.product.custom_attributes'):

        x['domain'] = value
        print(x)
        product[key.split(".")[2]] = x


    if key == 'start_date' or 'renewal_date':
        value = str(value)
        product[key] = value
return product

In this part below, how do make sure that the value is not a string when dumped?

if key.startswith('customer.product.custom_attributes'):

    x['domain'] = value
    print(x)
    product[key.split(".")[2]] = x

Because in the output I'm getting:

            {
                "custom_attributes": "{'domain': 'somedomain.com'}",
                "description": "some_description",
                "discount": "0.00"}

When what I really want is:

            {
                "custom_attributes": {"domain": "somedomain.com"},
                "description": "some_description",
                "discount": "0.00"}

EDIT: how i'm dumping:

with open('out.json', 'w') as jsonout:
    json.dump(main_json, jsonout, sort_keys=True, indent=4)

1 answer

  • answered 2018-03-14 19:01 Laur3ns

    Well, this IF is flawed and always TRUE:

    if key == 'start_date' or 'renewal_date':

    So you are converting everything to str()