JSON - I am obviously missing something

JSON - I am obviously missing something
0.0 0

#1

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?

var responses =
{“search”: {“entry”:
[
{“dn”: "uid=123456789,c=us,ou=pages,o=yahoo.com”,“attribute”:
[
{“name”: “notesid”,“value”: [“CN=John Doe/OU=Houston/O=USA”]},
{“name”: “email”,“value”: [“jd@yahoo.com”]},
{“name”: “callupname”,“value”: [“Doe, Johnathan (John)”]},
{“name”: “uid”,“value”: [“123456789”]}
]
}
],
“return”: {
“code”: 0,
“message”: “Success”,
“count”: 1
}
}
}

Any/all help/insight is appreciated :slight_smile:


#2

Check your quote marks. You’re using “fancy quotes” on some of the string values and it’s causing JSON to not successfully verify.

for example, at end of USA. Look at your quote symbol.

Here’s a valid JSON

{
	"search": {
		"entry": [{
			"dn": "uid=123456789,c=us,ou=pages,o=yahoo.com",
			"attribute ": [{
					"name": "notesid",
					"value": ["CN=John Doe/OU=Houston/O=USA"]
				}, {
					"name": "email",
					"value": ["jd @yahoo.com "]
				},
				{
					"name": "callupname",
					"value": ["Doe, Johnathan(John)"]
				},
				{
					"name": "uid",
					"value": ["123456789"]
				}
			]
		}],
		"return": {
			"code": 0,
			"message": "Success",
			"count": 1
		}
	}
}

#3

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.


#4

You’re missing several levels. Hint: look out for square brackets - anything inside is a list (even if the list contains only one entry).

Spoiler `console.log(responses.search.entry[0].attribute[3].value);`

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).


#5

You need to deprecate your square brackets surrounding your values.
You also need to get rid of your name attribute. The key IS the name.

{ "name: “key”,value: [“value”] } needs to change to { “key”: “value” }

to access the value following the latter example, simply use the designated key.
in example:

console.log(reponses.email) // jd@yahoo.com
responses.email = "new@yahoo.com" // responses.email === "new@yahoo.com"


#6

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.


#7

Thanks. That’s good advice. I have a solution now that I’ve implemented.


#8

Sorry, yeah, array not list. Guess I’m stuck in Python terminology mode. :stuck_out_tongue:

Damn, that’s a shame. Someone needs to have a few words with that API designer. :joy:


#9

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!


#10

Cool. Thanks for sharing. Will have to look into it just in case.