I am requesting data from an entity using the SG REST API, and our fields that were configured to be “float” fields are returning their data as string. This is inconsistent with the Python API, which returns the target data from the field-in-question as a Float.
I am requesting data from
some_field_name is of type Float in SG.
When reviewing the JSON response,
data.attributes.some_field_name is formatted as a JSON string.
Is this a bug or a feature?
Is this because JSON does not have a true float representation?
I would like to point out that this design is inconsistent with how the Python API operates
response = sg.find_one("<EntityType>", [["id", "is", <EntityId>]], ["some_field_name"])
some_field = data["some_field_name"]
# Result: 1.0 <type 'float'>
Just chiming in to confirm this behavior. Via the REST API I’m currently de-serializing floats as string type, and then additionally parsing them to get their actual float value.
Not ideal, but here we are.
Edit: Worth noting that depending on your preferred language and framework, libraries like
Newtsonsoft.Json will happily de-serialize a string-float like the above to both
double without complaining.
All HTTP parameters arrive as strings, so I suppose the REST API does not do any conversion on them? Can you confirm this is the case with fields other than floats?
The Python API has knowledge of each entity’s fields and types, which REST probably does not.
You could possibly read the entity’s schema and decide what needs to be converted.
The API should probably do this, but does not.
If I understood @bzenker correctly, he’s referring to the JSON payload returned by the REST endpoint in question. The data is sent as plaintext, specifying encoding
This is a sample shot I pulled out to demonstrate:
"code": "Test shot"
Ok so indeed the int is passed as is, but the float is quoted. This should actually be working for floats - they are allowed in json. Might be a bug in the api.
Correct. Thank you for clarifying for me @danielskovli. I was conflating JSON decoding with what gets communicated over HTTP.
And to @mmoshev’s point, I was previously incorrect in stating that JSON does not have a float representation. After re-reading the spec and then checking https://jsonlint.com/ it would appear that it should be totally valid for the SG REST API to return
So, I would agree that this appears to be a bug.
I am also currently parsing floats after receiving their string value as @danielskovli suggested, as an interim solution.