I am attempting to get values from a returned JSON object form a REST service. When I attempt to get the number of "rows" or "records" -- not the number of keys -- I continually get undefined
when using Object.keys(json).length
.
I have reviewed and tried a few of these answers on these posts:
Get total number of items on Json object?
Here is the code I am using:
$.getJSON(finalURL, function(json) {
console.log("json: " + json);
console.log("json.length: " + json.length);
var propertyNames = Object.keys(json).length;
console.log("propertyNames.length: " + propertyNames.length);
var jsonLength = propertyNames.length;
console.log("jsonLength: " + jsonLength);
if (isNaN(jsonLength)) {
console.log(jsonLength + " is not a number");
} else {
console.log(jsonLength + " is a number");
}
if (jsonLength > 0) {
console.log("jsonLength > 0");
} else {
console.log("jsonLength = 0");
}
});
Here is the returned JSON:
{"id":1,"providerName":"Acme","providerID":"12343","providerLegacyID":"832940","contactName":"John Doe","contactEmail":"jdoe@this.eml","contactPhone":"3035551212","address1":"5999 Second Street","address2":"","city":"Denver","state":"CO","zip":"80203","providerKey":"0be32d8057924e718a8b6b4186254756","userKeys":null,"approved":null,"active":null,"createdBy":"Dan Zeller","createdByKey":"c6f4cf6a47a44092a3655420bd4a3f26","createdByRole":"ADMIN","createdDate":1517927130501,"updatedBy":null,"updatedByKey":null,"updatedByRole":null,"updatedDate":null,"removedBy":null,"removedByKey":null,"removedByRole":null,"removedByDate":null,"restoredBy":null,"restoredByKey":null,"restoredByRole":null,"restoredByDate":null}
So I am expecting the length of the JSON object to be 1 with the above JSON. If more are returned, that number would go up.
Here is the output from the code:
json: [object Object]
json.length: undefined
propertyNames.length: undefined
jsonLength: undefined
undefined is not a number
jsonLength = 0
Appreciate any help.