Trying to get the values in the JSON below, say for “name”:“uid” but stuck on how to get there. I have the JSON going into a var responses and going to responses.search.entry.attribute.uid to retrieve “123456789”, but not working. Any suggestions?
Thanks, owel. I checked and corrected the fancy quotes. Still not retrieving the value for uid. I suspect my syntax is incorrect when I do responses.search.entry.attribute.uid.
As a more general note, assuming you’re designing the format of the JSON objects, you might want to rethink it. There’s a reason it’s so convoluted to get the data you need from it. In particular, note that JSON attributes should already be in the format name: value, such as "color": "red". However, in several places you have it more like {"name": "color", "value": "red"} (i.e. {name: value, name: value} for a single attribute).
Hi, thanks. Yes, I had a small “eureka” last night when it hit me that “attribute” is an array. Once I had that epiphany, I was able to develop a solution by looping through the objects in “attribute” to get the values I needed. Unfortunately, I am not designing the format of the JSON object but rather that is what the API response is from the app which I am getting the data. Thanks for the help.
Not sure if this help, but if you ever encounter an API set up in this way, and want to fetch data from it, there is a lodash method I believe called _.mapKeys which can pull out a single key from an object (even if it’s in an array) and make the resulting object ( a new key:value pair). I have used this often when working with very convoluted JSON in my React projects-- works well for me!