AndyJ
AndyJ

Reputation: 143

Passing a Python string into JSON payload

I have this in my code:

targetTemp = 17
...
payload = "{\n    \"nodes\": [{\n        \"attributes\": {\n            \"targetHeatTemperature\": {\n                \"targetValue\": '+ targetTemp +',\n            }\n        }\n    }]\n}"

I've tried a few things I've found online but nothing seems to work. If I replace '+ targetTemp +', with, say, 18 then it does what I want.

I've tried just with single quotes and no plus signs, just the variable name, without the comma at the end. I'm just stumbling round in the dark, really.

Upvotes: 5

Views: 17570

Answers (4)

Karl
Karl

Reputation: 1714

The reason your string isn't working is because you used double quotes " for the string instead of single quotes '. Since json format requires double quotes, you only should be using double quotes inside the string itself and then use the single quotes to denote the start/end of a string. (That way you don't need to keep using those \" unnecessarily.
Also, .format() can help with putting variables inside strings to make them easier.

This should fix your json string:

targetTemp = 17

payload = '{\n    "nodes": [{\n        "attributes": {\n            "targetHeatTemperature": {\n                "targetValue": {},\n            }\n        }\n    }]\n}'.format(targetTemp)

However, using the json module makes things a lot easier because it allows you to pass in a python dictionary which can be converted from/to a json string.

Example using the json package:

import json

targetTemp = 17

payload = {
    "nodes": [{
        "attributes": {
            "targetHeatTemperature": {
                "targetValue": targetTemp
            }
        }
    }]
}

payload_json = json.dumps(payload)  # Convert dict to json string

Upvotes: 9

Sufiyan Ghori
Sufiyan Ghori

Reputation: 18753

Your payload is awfully complex, you don't need to enclose it in quotes since it can be treated as a dict, also your targetTemp is being treated as a string this is why you don't see the actual value.

You may need to look into Python string formatting for simplicity.

This would do what you want,

import json

targetTemp = 17
payload = {
    "nodes": [{
        "attributes": {
            "targetHeatTemperature": {
                "targetValue": targetTemp
            }
        }
    }]
}

print(json.dumps(payload))

# output,
{"nodes": [{"attributes": {"targetHeatTemperature": {"targetValue": 17}}}]}

Please note that you can also use JSON Validators to make sure your json object is indeed in a correct format.(that's what I used to format your provided JSON)

Upvotes: 4

jwodder
jwodder

Reputation: 57470

Let's shorten that code down a bit.

payload = "{...\"targetValue\": '+ targetTemp +'...}"

See the problem now? payload is delimited with double quotes, so in order to exit, append to, and "re-enter" the string, you need to use double quotes instead of single quotes:

payload = "{...\"targetValue\": "+ targetTemp +"...}"

Alternatively, the far more robust and less tedious solution would be to build up payload as a dict of Python structures containing targetTemp as a normal value, and then after that serialize it with json.dumps:

payload_obj = {
    "nodes": [{
         "attributes": {
             "targetHeatTemperature": {
                 "targetValue": targetTemp,
             }
         }
    }]
}

payload = json.dumps(payload_obj)

Upvotes: 2

OneCricketeer
OneCricketeer

Reputation: 191743

'+ targetTemp +' within the outermost double quotes isn't doing string concatenation. It's literally putting that text.

You should be using "+ targetTemp +"

However, building an actual dictionary, and using json.dumps will be less error-prone

Upvotes: 5

Related Questions