Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to represent null and true i json payload Python

I have a payload in my request against an endpoint having values null and true. In Pycharm I get red-dotted line below the two respectively. How can I "define null and true inside my json payloads setmethod in Python?

def setpayload_set():
myjson = {
    "hpsId": 10034,
    "powerPlants": null,
    "units": [{"componentId": 78111,
               "timeSeries": [{"name": "ts.start_bids_allowed",
                               "dtssId": "", "tsv": {"pfx": true,
                                                     "data": [[1723647600,
                                                               1]]}}]}]
          }
return myjson

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

In python you should use None istead of null and True istead of true in your JSON-like dictionary.

def setpayload_set():
    myjson = {
        "hpsId": 10034,
        "powerPlants": None, 
        "units": [
            {
                "componentId": 78111,
                "timeSeries": [
                    {
                        "name": "ts.start_bids_allowed",
                        "dtssId": "",
                        "tsv": {
                            "pfx": True,  
                            "data": [
                                [1723647600, 1]
                            ]
                        }
                    }
                ]
            }
        ]
    }
    return myjson

but if you wont to put null and true as the values then convert the python dictionary to Json string.

import json

json_payload = json.dumps(setpayload_set())
print(json_payload)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading