Home > OS >  Get str representation of JSON to encode like print()
Get str representation of JSON to encode like print()

Time:10-22

I need to pass a JSON object that looks like below to a REST API (plz note the escaped " in the value - which is json in string format)

{ "data": "{\"positionname\": \"API User\",\"person\": \"Paul\",}" }

In POSTMAN, using POST method to API endpoint with JSON above, I get 200 and a record created. When I try to do the same from Python Shell using 'requests', it fails:

# JSON as string with proper escape
>>> data = '{"data": "{\\"positionname\\": \\"API User\\", \\"person\\": \\"Paul\\"}"}'
# Loads output removes backslashes
>>> json.loads(data)
{'data': '{"positionname": "API User", "person": "Paul"}'}
# Print output shows data -exactly- as I need to pass it to the API
# The string that print() provides is in the EXACT format that the API requires
# It needs " around data, and the escape \ character is needed.
>>> print (data)
{"data": "{\"positionname\": \"API User\", \"person\": \"Paul\"}"}
# Data still in original format...
>>> data
'{"data": "{\\"positionname\\": \\"API User\\", \\"person\\": \\"Paul\\"}"}'
# Try to pass the data to the API..
>>> response = apiRequest.post('http://apiURL/board/Input', data)
# It fails...
>>> response<Response [400]>
>>> 

UPDATE:

I was able to use POSTMAN to add a record using single-quotes as show below. However, when I try in Python, the "data" key is changed to 'data' (single quotes) and the API requires double-quotes:

# Now using single quotes in the JSON - sending this to POSTMAN works
>>> data = '{"data": "{\'positionname\': \'API User\', \'person\': \'X\'}"}'

# Data still looks great.
>>> data
'{"data": "{\'positionname\': \'API User\', \'person\': \'X\'}"}'

# But when I send to the API, I get an error saying that double-quotes are required at the first character - that was odd b/c they are - but when I do a json.loads() I see that the "data" is now 'data'

Error:
json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)

>>> json.loads(data)
{'data': "{'positionname': 'API User', 'person': 'X'}"}

^^^ why is 'data' singlequoted and the value is "{}" doublequoted?

CodePudding user response:

You don't need to "trap" the output of print(); you already have the data.

print() prints the actual data as it really exists, which is distinct from the repr()esentation of that data encoded in Python syntax.

Similarly, a call like file.write(yourdata) or socket.send(yourdata) will send that literal data, not the Python representation thereof.


If it's in your control, the logic you use to generate your string should look like this:

content = { "positionname": "API User", "person": "Paul" }
wrapper = { "data": json.dumps(content) }
wrapper_json = json.dumps(wrapper)

...if you print(wrapper_json), you'll see something formatted in exactly the right way to send to your service.

CodePudding user response:

Your use case looks strange, but if you don't find any satisfactory solution you can try this to get desired output.

data = '{ "data": "{\\"positionname\\": \\"API User\\",\\"person\\": \\"Paul\\",}" }'

dataSplitted = data.split("\\")

output = ""

for word in dataSplitted:
    output = "{}\{}".format(output, word)

output = output[1:]
  • Related