Deterimne Javascript Object Field Names

gdpotter
gdpotter used Ask the Experts™
on
I have a project I am working on which loads in a JSON file.  I quickly convert that JSON file to a Javascript object using jquery with $.parseJSON();.  What I need to do now is to spit out what the field names are.  For example, if my data is this:
[
{
field1 : 'field1data',
field2 : 'field2data',
field4 : 'field4data'
},
{
field1 : 'field1data',
field3 : 'field3data',
field4 : 'field4data'
}
]

Open in new window


I want to take the field names and load them into an array.  The result in my example would be ['field1', 'field2', 'field3', 'field4'].  I need it to get ALL of them, even if they don't appear in every object.

If possible, I would like to do this without parsing the whole file.  Is there a way I can do this using JavaScript (and maybe jQuery) methods?

Thanks!
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Try:

var c=0;
var fields=new Array();

for (a in object)
  fields[c++] = a;

leakim971Multitechnician
Top Expert 2014

Commented:
You may use :

var names = [];
var fields = [];

for(i=0;i
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script language="javascript">
var json = [
{
field1 : 'field1data',
field2 : 'field2data',
field4 : 'field4data'
},
{
field1 : 'field1data',
field3 : 'field3data',
field4 : 'field4data'
}
]

var names = [];
var fields = [];

for(i=0;i<json.length;i++) {
	for(name in json[i]) {
		if(typeof fields[name] == "undefined") {
			fields[name] = true;
			names.push(name);
		}
	}
}

// display all name :

alert( names.toString());


</script>
</head>

<body>
</body>
</html>

Open in new window

Multitechnician
Top Expert 2014
Commented:
Note : we parse the whole file
Another one :

      Array.prototype.has = function(v) {
            for(_i=0;_i
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script language="javascript">
	var json = [
	{
	field1 : 'field1data',
	field2 : 'field2data',
	field4 : 'field4data'
	},
	{
	field1 : 'field1data',
	field3 : 'field3data',
	field4 : 'field4data'
	}
	];

	Array.prototype.has = function(v) {
		for(_i=0;_i<this.length;_i++) if(this[_i]==v) return _i;
		return -1;
	}

	var names = [];
	var fields = [];
	
	for(j=0;j<json.length;j++) for(name in json[j]) if(names.has(name)<0) names.push(name);

	// display all name :
	
	alert( names.toString());

</script>
</head>
<body>
</body>
</html>

Open in new window

Should you be charging more for IT Services?

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
This works great, but could you please explain WHAT it is doing?  I understand that it looping through the whole object, then it checks to see if the field was added already?  Is this correct?
leakim971Multitechnician
Top Expert 2014

Commented:
We extend the Array object with the function has
This function return the indice of a value v in the array
Array.prototype.has = function(v) {
we parse the array with a loop and if the current value equal to the searched value v we return the current indice
           for(_i=0;_i
If no value is returned, we return << -1 >>
           return -1;

We build an empty array for the names
      var names = [];

For each JSON object in the array of JSON object << json >>
     for(j=0;j
We parse the current object, the indice is name, json[j] is the current JSON object in the array of JSON object << json >>
for(name in json[j])
If the name is not found in the array of name << names >> we add it in the array of names with : names.push(name)
if(names.has(name)<0) names.push(name);

Author

Commented:
Thank you so much.  You were very helpful.
leakim971Multitechnician
Top Expert 2014

Commented:
You're welcome! Thanks for the points!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial