'Add variable value in a JSON string in Python
I am puzzled why this is not working. I am trying add my variable value in the JSON and everytime I add it, it is not showing properly in my JSON string.
hostname = "machineA.host.com"
I need to add the above hostname information to the below JSON document -
b"{\"Machine Name\":\"\"+hostname+\"\"}", None, True)
But whenever I am adding it in the above way, it is not working at all.
Not sure what wrong I am doing here?
Solution 1:[1]
instead of manipulating the string consider having the data as a python structure and then dump it to json
>>> d = {}
>>> d['Machine Name'] = hostname
>>> json.dumps(d)
'{"Machine Name": "machineA.host.com"}'
Solution 2:[2]
Please see this example you will get some idea.
test=f"Your order total is $ 150."
{"Total":"'+test+'"}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Guy Gavriely |
| Solution 2 | Riyaj Shaikh |
