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
Answer from Karl on Stack OverflowThe 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
'+ 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
Convert string to JSON in Python? - Stack Overflow
pass a json string as an argument to Python script causes quotes problems - Stack Overflow
Sending my python object as proper JSON text
How to retrieve JSON information through jinja
What's the fastest way to parse JSON in Python?
Can json.loads() parse bytes directly in Python 3?
How do I validate a JSON string against a schema?
Your code will work as expected if you change how you assign your input variable. Change this line:
inp = parser.parse_args()
to
inp = args.inputstring
parse_args() returns an argparse.Namespace object, so you need to retrieve the input from that object prior to passing it to the parser. In addition, you will need to escape the double quotes in your shell command. I get the expected output with the above change run with:
python myscript.py -i "{ \"Employees\": \"name name\"}"
You should escape the internal double quotes in the JSON:
python myscript.py -i "{ \"Employees\": \"name name\"}"
But looking at your code, there are a couple of other reasons it may not be working:
you're assigning
argsandinpthe same value, return value of the function call:parser.parse_args(). Perhaps you meant to assigninp = args.inputstringyou've tagged the question as python2 but looking at your
print(...)statement, it looks like you've written for python3