Generate dynamic jason with dictionaries/list in python

498
2
02-21-2022 07:43 AM
nexus5
by
New Contributor

HI i am trying to generate a jason paylod for an API which is querying the table in a database. The jason paolod structure is as follows. 

{
    "condition":"AND",
    "rules": [
        {
            "condition": "AND",
           "rules": [
                {
                    "field": "column1",
                    "value": "4861868",
                   "operator": "equal"
                       }
                ]
         }
   ]
}

Structure of Jason for multiple columns input

{
           "condition":"AND",
           "rules": [
              {
                "condition": "AND",
                "rules": [
                     {
                        "field": "column1",
                         "value": "4861868",
                         "operator": "equal"
                       },
                     {
                          "field": "column2",
                          "value" : "2018-12-01"
                          "operator": "equal"
                      }
               ]
        }
   ]
}

 

There are multiple columns in the table and the Jason should be generated for any arguments that are passed to the script. The script will take the values for those columns as input . What is the best possible way to store the inputs and can you please help me how can I generate a dynamic jason for this ?

Column1Column2column3column4
4861868HUIOIN2108-18-51NRIUNRI
486849498VSMVOIRJ1956-12-05VMPEIORP
85895995IPOPROER1965-12-16VNEIORV

 

These columns are kind of optional to user. Only Column1 is a mandatory argument to the script. Please kindly help me how can i generate Jason for this kind of condition

0 Kudos
2 Replies
nexus5
by
New Contributor

@JoshuaBixby  . Can you please help 

0 Kudos
DonMorrison1
Occasional Contributor III

I usually construct the payload as a python dict then convert the dict to json using the technique described here