Covert Array into Object

Options
Hi, i receive an array of choices from an API, like the example below: 
Tags: ["Marketplace","Cryptos","Real State"]

I would like to insert the response of the API i have called in a Object field. How i can do that?

Comments

  • Umair Kamil
    Umair Kamil Member
    Options
    • Concept 1: Example of Tags as an Object


    Tag {
    "content_tag": "Marketplace",
    "niche_tag": "Cryptos",
    "category_tag: "Real Estate
    }
    • Concept 2: Example of Tags as an array of primitives or array of string values


    Tags: ["Marketplace","Cryptos","Real State"]

    Note: objects require key value pairs. For example

    Tag {
    "content_tag": "Marketplace",
    "niche_tag": "Cryptos",
    "category_tag: "Real Estate
    }

    The keys here are 
    • content_tag
    • niche_tag
    • category_tag


    The values here are 
    • Marketplace
    • Cryptos
    • Real Estate


    Conclusion
    • BUT in your case the "tags" object from the API has NO SCHEMA, as it has NO KEYS. It's just a list of values.

    Solution
    • Therefore, from what you are saying, I would NOT suggest storing tags as an object
    • I would (instead) suggest that in your database, you delete the "tags" object field and replace it with a "tags" text list field
    • You are suppose to use "objects" in the database when you intend to store key_value pairs. For example


    Detailed Example
    • post_name --> pushed to "name" text column in database
    • post_metadata: {"velocity": 100, "range": 22} --> pushed to "posts_metadata" object column in the database

    BUT your case is more like
    • post_name --> pushed to "name" text column in database
    • post_tags: ["Marketplace","Cryptos","Real State"]--> pushed to "post_tags" object column in the database


    Then, when you map your "api.response.results.posts.tags" to the "tags" text list field, the data will successfully go through
  • Moises Amselem
    Options
    Hi Umair, thanks a lot for your answer that was very clear. I changed the filed to text list and it worked perfectly!