0

I retrieved the data from the database which was in [object Object].

I usually convert these data using JSON.parse but it did not working so, I tried JSON.stringify but the data now it's like

[{"station" : "NewYork"}, {"station" : "Toronto"}]

Now I don't know how to convert this data into an array object and access it. I need only object entries but I don't know how to access it.

If it is declared to var data I need it like data[0]['station'] to access but I couldn't do it.

Ajeet Shah
  • 18,551
  • 8
  • 57
  • 87
  • 1
    Try `data[0]['station']` without `JSON.stringify` or `JSON.parse` – Sajeeb Ahamed May 24 '20 at 11:37
  • No, Initially it's in this format [{object Object},{object Object}] so I converted, I tried even your method it's showing undefined. – philips funds May 24 '20 at 11:51
  • `[object object]` is default serialization of an object. [more](https://stackoverflow.com/a/25419538/2873538) – Ajeet Shah May 24 '20 at 11:53
  • `I retrieved the data from the database`: How? From an API response? How does your API respose look in browser? It should look something like [this](https://i.stack.imgur.com/wlyCE.png). – Ajeet Shah May 24 '20 at 11:57
  • I'm getting a response like `{"message":[{"station_name":"Hebbal"},{"station_name":"Jalahalli"},{"station_name":"Malleshwaram"},{"station_name":"Peenya"},{"station_name":"R.T.Nagar"},{"station_name":"Rajajinagar"}]}` @AjeetShah – philips funds May 24 '20 at 12:08
  • It looks in correct, JSON, format. Now show us code where you are trying to access it. You can add that in your question by editing it. – Ajeet Shah May 24 '20 at 12:10

0 Answers0