Home > OS >  Create json from python dict
Create json from python dict

Time:12-19

I try to create a peace of JSON from a python dict. Unfortunately I am not able to get the correct format yet. I need a array in my json object 'types'. How can I fix this?

My code:

import json
association_item = {}
association_item['types']={}
association_item['types']['associationCategory'] = 'A'
association_item['types']['associationTypeId'] = '4'

My dict:

{'types': {'associationCategory': 'A', 'associationTypeId': '4'}}

My JSON:

{
    "types": {
        "associationCategory": "A",
        "associationTypeId": "4"
    }
}

What i want:

{
    "types": [{
        "associationCategory": "A",
        "associationTypeId": "4"
    }]
}

CodePudding user response:

You need to add it correctly, like

association_item['types'] = 
    [ { 'associationCategory': 'A',
        'associationTypeId': '4' } ]
  • Related