push data in to json object

rick48
Tera Contributor

need to push the data into json object and store it in a variable

function executeRule(current, previous /*null when async*/) {

	var fields = current.getFields();
	var sysid=current.sys_id;
	var json={};
	
	var hits = [];
	var value=[];
	var field=[];

		for (var i = 0; i < fields.size(); i++) {
			var glideElement = fields.get(i);
			if (glideElement.hasValue()) {
				field.push(glideElement);
				value.push(glideElement.getName());
				hits.push(glideElement.getName() + ':' +glideElement);
				json.push(glideElement.getName() + ':' +glideElement);
		}
	}


var temp = JSON.stringify(json);
gs.log(temp,"jidesh");
	hits.push("sys_id" + ":" + sysid);
	
//
})(current, previous);

tried above script but it didn't work

in logs its getting as below

find_real_file.png

where am i gone wrong?

6 REPLIES 6

Anurag Tripathi
Mega Patron
Mega Patron

Add everything in your arraye and then you can encode it into json as below

new global.JSON().encodeArray(<your array>);

-Anurag

its in the form of array not json and if i make hits = {}(curly braces)  it returns []

find_real_file.png

Ok, the simpler way

var myObj={}

myObj.name='AAA';

myObj.title='BBB';

myObj.something='CCC'l

 


var json = new JSON();
var result = json.encode(myObj);//JSON formatted string

 

Please mark my answer correct/helpful if it helped you solve your issue.

-Anurag

-Anurag

since i cant get all the fields and values from incident table i just used a forloop and tried to push it to the json.

 it need to be pushed dynamically.