Help forum

JSON response to array

  • TCG.Store

    2 years ago

    Has anyone taken a JSON response from auth API and stored the result into an array in toddle? I was trying to see if there are some best practices?
  • Max

    2 years ago

    That depends very much on your data structure. Can you give an example of the data you want to store?
  • TCG.Store

    2 years ago

    {
    "user": {
    "user_id": "1234",
    "email": "abc@toddle.dev",
    "social_id": "",
    "first_name": "Tim",
    "last_name": "Cook",
    "created_at": 1726023688,
    "patreon_access_token": null,
    "is_store": false
    },
    "auth": {
    "access_token": "6789",
    "refresh_token": "",
    "expires_in": 2419200
    }
    }
  • This is an example of what is returned from the API
  • Max

    2 years ago

    I see. Why do you want to store it as an array? An array is a list of items. This seems to be a single record.
  • TCG.Store

    2 years ago

    It's returned from an auth API, and basically want to store the access token and use this to query speciifc users given a few things we want to do
  • TCG.Store

    2 years ago

    This is just an example. It happens to be a single record but either way love to store a specific pieces of an object from an arrary or a single record
  • TCG.Store

    2 years ago

    if that makes sense
  • Max

    2 years ago

    For auth tokens, I would use the set session cookie action from Toddle. This is more secure. Also, variables are reset on refresh or when a component is unmounted.

    For normal data, this depends very much on the data itself. You can store anything from an API in a variable as is. Or you can store only specific parts of it. Depends on your use case
    🔥1
    🙏1
  • TCG.Store

    2 years ago

    Thanks for this suggestion and this makes sense.

    I was trying to see if there were an examples of storing this data in variable and best way to access this data in a different place?
  • For example I would like to extra the auth token returned into a variable to than use to set the cookie and also use it for other endpoints instead of consistently filtering out this entire single record per use case. If that makes sense
  • TCG.Store

    2 years ago

    Following up here

    I got it all saved to a variable and it's working successfully!
    🙌1