0

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:

Length of a JavaScript object

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.

Dan
  • 940
  • 2
  • 14
  • 42

2 Answers2

1

Object.keys(json) returns an array, so its .length is a number.

You're trying to get the .length of a number, which makes no sense.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • Is there another way to correct this? Is there a sample I could use? So, using `var propertyNames = Object.keys(json).length;` will not work? Thanks – Dan Feb 07 '18 at 16:45
  • @Dan: Why do you think that won't work? Your problem is that `.length.length` makes no sense. – SLaks Feb 07 '18 at 17:04
  • `var propertyNames = Object.keys(json).length;` returns `32` but what I am looking for is to return `1` since I see only one "record" returned. If 2 were returned, I am looking for `2` to be returned. I appreciate the help! – Dan Feb 07 '18 at 17:26
  • @Dan: That's not how JSON works. It sounds like you're describing an array, but you don't actually have an array. – SLaks Feb 07 '18 at 17:59
0

you can use somethink like this :

  var myObject = {"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};

  var count = Object.keys(myObject).length;
  console.log(count);
Léo R.
  • 2,620
  • 1
  • 10
  • 22
  • So, the `json` object I am returning will have to be set to `myobject` to work? Something like `var myObject = json;`? Thanks – Dan Feb 07 '18 at 16:48
  • No.. myObject is just an example to define your json , cause i tested on plnkr and i miss your part of code wich return json – Léo R. Feb 07 '18 at 16:53
  • you can do this : var count = Object.keys(json).length; – Léo R. Feb 07 '18 at 16:53
  • I tried defining the `myObject` in my code and still got `undefined` when trying to get the length. Would is be something in the JSON formatting? The code to return the JSON is in a Play Framework controller: `public Result restGetProvider(String providerKey) { Provider provider = Provider.findByProviderKey(providerKey); ObjectMapper mapper = new ObjectMapper(); JsonNode jsonData = mapper.convertValue(provider, JsonNode.class); System.out.println("jsonData: " + jsonData); return ok(jsonData); }` – Dan Feb 07 '18 at 16:54