OussamaLord
OussamaLord

Reputation: 1095

Javascript, count number of key/values inside stringified JSON Object

Hello I have an object that I'm convertion into JSON like that :

var myJSON = JSON.stringify(myObject);

the console.log(myJSON); gives :

[{"RowID":"6","Name":"joe","Alias":"ss","Email":"[email protected]","Phone":"123456"}]

How can I count the number of keys inside that json? I want to count 5 items as a result :

RowID, Name, Alias, Email, Phone

I tried to count the elements within the object before the JSON stringify with : console.log(Object.keys(myObject).length); but it gives me 1 not 5

How can I do that?

Upvotes: 1

Views: 5164

Answers (1)

Joseph
Joseph

Reputation: 119827

That's because myObject is an array. You need to access the first item of the array to get your 5-keyed object.

Object.keys(myObject[0]).length

Upvotes: 4

Related Questions