All information from API comes as strings

Jake_McJake_Mc Member Posts: 18

My developer informs me that all information from the API comes as a string.

This is not very convenient. Is there a reason for this, or something we can do to fix? '


  • LucienVersendaalLucienVersendaal Moderator, Lightspeed Staff Posts: 882 moderator

    Hi @Jake_Mc,

    Thank you for reaching out to us.

    This the standard JSON API response, we can't change that. Please check for more information.

  • gregaricangregarican Member Posts: 702 

    @Jake_Mc I personally don't see this being a major issue, as fields are usually documented against what data types they are conveying. So any developer can cast/convert the values if they need to (e.g. - a record ID into a long value, an "updated at" field into a datetime value, etc.).

  • sam123456sam123456 Member Posts: 54

    I also found it kind of annoying to deal with so I use this little piece of code to convert them for me. It helps me out when they switch it up between list/dict which sometimes happens.

    in python:

    class Decoder(json.JSONDecoder):
        def decode(self, s):
            result = super(Decoder, self).decode(s)
            return self._decode(result)
        def _decode(self, o):
            if isinstance(o, str):
                    return int(o)
                except ValueError:
                        return float(o)
                    except ValueError:
                            return bool(strtobool(o))
                        except ValueError:
                            return o
            elif isinstance(o, dict):
                return {k: self._decode(v) for k, v in o.items()}
            elif isinstance(o, list):
                return [self._decode(v) for v in o]
                return o

  • Jake_McJake_Mc Member Posts: 18

    Thanks @gregarican @LucienVersendaal and @sam123456 for your input :)

Sign In or Register to comment.