1

I am reading JSON response into JS object. JSON source is like this:

{
  "id": 1,
  "email": "info@test.com",
  "created_at": "2013-01-15 18:19:00.000000",
  "updated_at": "2016-04-27 09:13:40.000000",
  "user_profiles": {
    "data": {
      "first_name": "Mark",
      "last_name": "Webber",
      "birthday": "30.10.1979",
      "company": "Company d.o.o.",
      "phone": "",
      "mobile": "+386 123 123 123",
      "vatid": "1234567",
      "custom_fields": null,
      "deleted_at": null
    }
  }
}

I can access the properties like:

myObject.email
myObject.user_profiles.data.firstname

Is there a way to access nested properties via string? Something like

var property = "user_profiles.data.firstname"
myObject[property]
Primoz Rome
  • 10,379
  • 17
  • 76
  • 108
  • `myObject['user_profiles']['data']['first_name']` would produce the string `Mark` – Derek Pollard Apr 27 '16 at 17:05
  • Yes, on the surface, that's just the difference between dot notation `.` and bracket notation `[prop]` – ann0nC0d3r Apr 27 '16 at 17:07
  • myObject["user_profiles"]["data"]["first_name"]. If you need it generic, then you could split your input string and recursively check or something. But, if you know the structure ahead of time, then there's no need for that kind of interrogation. – ManoDestra Apr 27 '16 at 17:07
  • I am having this dynamic component that renders data table from array of these objects. I would like to add the path to the nested property and get the vallue... What you are suggesting works yes but, need something like getColumnValue("user_profiles.data.firstname") – Primoz Rome Apr 27 '16 at 17:07
  • Felix beat me to it - here is another duplicate: http://stackoverflow.com/questions/8051975/access-object-child-properties-using-a-dot-notation-string – mplungjan Apr 27 '16 at 17:08
  • If you know the structure, then why not simply use myObject.user_profiles.data.first_name. That works fine. You're only going to need something like this, if your object model is unknown and you need dynamic access, but that's not likely. – ManoDestra Apr 27 '16 at 17:09
  • @mplungjan thanks for the link, this looks like something I am after... Will try – Primoz Rome Apr 27 '16 at 17:14
  • This is perfect solution http://stackoverflow.com/a/33397682/255710 – Primoz Rome Apr 28 '16 at 07:37

1 Answers1

1

How about this

myObject['user_profiles']['data']['firstname']
baklazan
  • 814
  • 6
  • 13
  • I am having this dynamic component that renders data table from array of these objects. I would like to add the path to the nested property and get the value... What you are suggesting works yes but, need something like getColumnValue("user_profiles.data.firstname") – Primoz Rome Apr 27 '16 at 17:09
  • @PrimozRome The path is going to be the same for every element in the array, and you already know the path to the property in question, so why would you need it to be dynamic like this? If you don't know the property you want, then there's no point trying to get it, except in a loop. And if you do know it,then just use the dot (or object['property']) notation. See what I mean? It wouldn't be difficult to split a single fully qualified property and loop down to the required level to return the value though via a small function. I just don't see the need for that. – ManoDestra Apr 27 '16 at 17:10
  • @ManoDestra: yes I know the path buy my table component is very dynamic. It goes thought the list of defined columns and I need to get the value for every column. So something like getColumnValue("email") on the first level, or getColumnValue("user_profiles.data.firstname") for the nested properties... – Primoz Rome Apr 27 '16 at 17:12
  • You can still achieve this for each property you need by using the normal object notation. – ManoDestra Apr 27 '16 at 17:14