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":"[email protected]","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.

1
  • 1
    console.log("json: ", json); should give you better idea what that object is Commented Feb 7, 2018 at 16:32

2 Answers 2

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.

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

4 Comments

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: Why do you think that won't work? Your problem is that .length.length makes no sense.
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: That's not how JSON works. It sounds like you're describing an array, but you don't actually have an array.
0

you can use somethink like this :

  var myObject = {"id":1,"providerName":"Acme","providerID":"12343","providerLegacyID":"832940","contactName":"John Doe","contactEmail":"[email protected]","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);

4 Comments

So, the json object I am returning will have to be set to myobject to work? Something like var myObject = json;? Thanks
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
you can do this : var count = Object.keys(json).length;
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); }

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.