1

I have a large JSON object which is being returned from QuickBooks Online API. It is valid JSON and shows up on the console (after logging it).

Upon inspecting the console, I see the following:

[Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object]

Which can be expanded to:

[Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object]
0: Object
1: Object
2: Object
3: Object
4: Object
5: Object
6: Object
7: Object
8: Object
9: Object
10: Object
11: Object
12: Object
13: Object
14: Object
15: Object
16: Object
17: Object
18: Object
19: Object
20: Object
21: Object
22: Object
23: Object
24: Object
length: 25
__proto__: Array[0]

0: Object expands to:

0: Object
*_data: Object
$$hashKey: "object:82"
__proto__: Object

*_data: Object expands to:

Active: Array[1]
Balance: Array[1]
BalanceWithJobs: Array[1]
BillAddr: Array[1]
BillWithParent: Array[1]
DisplayName: Array[1]
FamilyName: Array[1]
Fax: Array[1]
FullyQualifiedName: Array[1]
GivenName: Array[1]
Id: Array[1]
Job: Array[1]
MetaData: Array[1]
Mobile: Array[1]
PreferredDeliveryMethod: Array[1]
PrimaryEmailAddr: Array[1]
PrimaryPhone: Array[1]
PrintOnCheckName: Array[1]
SalesTermRef: Array[1]
SyncToken: Array[1]
Taxable: Array[1]

I'm trying to access a property called DisplayName.

The Angular code that is being used to console.log the json data:

$http.get('/customer').success(function(customers) {
    $scope.customers = customers;
    console.log($scope.customers[0]["*_data"].DisplayName[0]);
});

How can this be done? Can something be typed in the console to return its value?

Update:

I can access the object using:

console.log($scope.customers[0]);

However, *_data is an object. I was thinking something like:

console.log($scope.customers[0].*_data); would work however, I receive the error:

Uncaught SyntaxError: Unexpected token *.

5
  • 1
    Can you do it with bracket notation? like <thisjson>[0]["*_data"]["DisplayName"]; ? Commented Dec 8, 2014 at 16:38
  • @brbcoding, I receive this error when trying: Uncaught SyntaxError: Unexpected token < And without the brackets, thisjson is not defined. Commented Dec 8, 2014 at 16:40
  • 3
    brbcoding didn't mean for <thisjson> to be used literally, just as a placeholder for whatever variable you store the json in. Commented Dec 8, 2014 at 16:47
  • RaphaelRafatpanah what @chazsolo said. Commented Dec 8, 2014 at 16:50
  • Ahh. console.log($scope.customers[0]); returns an object in the console. How can I access an object? console.log($scope.customers[0]["*_data"]); returns undefined. Commented Dec 8, 2014 at 16:58

2 Answers 2

5

Are you logging it to the console?

Assuming you are, and that you have something like:

console.log(data);

You can access the data like this:

data[0]["*_data"].DisplayName[0]

That is:

  1. Dereference the first array item with data[0]
  2. Then access the *_data property. Because its name is not a valid property name, you have to use the ["string"] form.
  3. The DisplayName value is an array. Take the first value with [0].

EDIT

Ok, wow this was actually quite an interesting one!

It seems that the response data contains a strange character which makes it tricky to access the response. Specifically:

> Object.keys($scope.customers[0])[0]
"*_data"
> Object.keys($scope.customers[0])[0] == "*_data"
false

Huh? Why aren't they equal? One of those characters must be a unicode char which looks identical but has a different value!

Anyway it's easy enough to work around this:

var mysteriousKey = Object.keys($scope.customers[0])[0];
var data = $scope.customers[0][mysteriousKey];

This gives me:

Object {Id: Array[1], SyncToken: Array[1], MetaData: Array[1], GivenName: Array[1], FamilyName: Array[1]…}

Which I think it what you were after.

Sign up to request clarification or add additional context in comments.

9 Comments

When trying, I receive Uncaught ReferenceError: data is not defined. It is being logged to the console.
Who logs it to the console? Is it your code that's making this request of the QuickBooks API?
Yes. I have an angular $http.get() request to my backend, which returns the json data. I immediately logged that data to the console. I also tried doing something this: console.log($scope.customers[0]["*_data"].DisplayName[0]); however, the console returns this error: TypeError: Cannot read property 'DisplayName' of undefined
Then you're getting close. I suggest you set a breakpoint there and inspect the data you're getting back. There's no way I can really help you from here. Perhaps the property name is _data instead of *_data?
Thank you. I do receive the right object when running console.log($scope.customers[0]); which returns Object {*_data: Object}*_data: Object__proto__: Object Perhaps the problem is because "*_data" is an object and not an array?
|
1

Your array should be assigned to a variable. Reference the index of the item in the array you want, and access it's DisplayName property (which is also an array).

var objArray; // this has your array of objects
objArray[0]["*_data"].DisplayName[0]; // should be the value you are looking for.

3 Comments

What if *_data is an object? How can it be referenced?
@RaphaelRafatpanah *_data even as an object would be referenced this way. It's referred to as square bracket notation as oppose to dot notation. Because this property has an *, you'll want to use the square bracket notation. However, if you property was for example _data you could do objArray[0]._data or objArray[0]["_data"]. Both produce the same result. For special characters like the asterisk though, it's better to use square bracket notation to avoid parser errors.
Thank you for your help and explanation. The reason it wasn't working was because of the * looked like an asterix but actually had a different value internally. See the chosen answer for details. Thanks again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.