Parsing JSON Response and set the value in the Variable of catalog form

tsoct
Tera Guru

Hello, everyone. Can someone show me where the problem is while receiving the Parsed value?

 

This is the response.

 

{"fields":[{"name":"ItemNumber","display_value":"895634"},{"name":"ItemName","display_value":"Apple Macbook Air"},{"name":"Type","display_value":"LAPTOP"},{"name":"Category","display_value":"PORTABLE LAPTOP"}]}

 

 

The catalog client script:

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    var itemNumber = g_form.getValue('item_id');

    var gaFields = new GlideAjax('AssetIntegrationAjaxUtils');
    gaFields.addParam('sysparm_name', 'retrieveAssetData');
    gaFields.addParam('sysparm_id', itemNumber);
    gaFields.getXML(showAssetData);

    function showAssetData(response) {

        var answer = response.responseXML.documentElement.getAttribute("answer");
        alert(answer); //Confirmed that alert is working fine.

        var obj = JSON.parse(answer); // Parsing
        g_form.setValue('item_name', obj.ItemName);
        g_form.setValue('type', obj.Type);
        g_form.setValue('category', obj.Category);
    }

}

 

1 ACCEPTED SOLUTION

Nishant8
Giga Sage

hello @tsoct , I assume that JSON is in the same format and if so, can you please modify your script as below and verify?

var obj = JSON.parse(answer); // Parsing
for (var i=0; i<obj.fields.length; i++){
    if(obj.fields[i].name.equals("ItemName"))
        g_form.setValue('item_name',obj.fields[i].display_value);
    if(obj.fields[i].name.equals("Type"))
        g_form.setValue('type',obj.fields[i].display_value);
    if(obj.fields[i].name.equals("Category"))
        g_form.setValue('category',obj.fields[i].display_value);          
 
}

View solution in original post

4 REPLIES 4

Nishant8
Giga Sage

hello @tsoct , I assume that JSON is in the same format and if so, can you please modify your script as below and verify?

var obj = JSON.parse(answer); // Parsing
for (var i=0; i<obj.fields.length; i++){
    if(obj.fields[i].name.equals("ItemName"))
        g_form.setValue('item_name',obj.fields[i].display_value);
    if(obj.fields[i].name.equals("Type"))
        g_form.setValue('type',obj.fields[i].display_value);
    if(obj.fields[i].name.equals("Category"))
        g_form.setValue('category',obj.fields[i].display_value);          
 
}

Brad Bowman
Kilo Patron
Kilo Patron

This object has an element named fields, the value of which is an array of objects.  Further, it seems like in the 3 setValue methods you want the value of the display_value element in the array item where the value of the name element matches ItemName, Type, or Category.   So it would look like this:

var fields = answer.fields;
for (var i = 0; i < fields.length; i++) {
	if (fields[i].name == "ItemName") {
        g_form.setValue('item_name', fields[i].display_value);
	}
	if (fields[i].name == "Type") {
        g_form.setValue('type', fields[i].display_value);
    }
	if (fields[i].name == "Category") {
        g_form.setValue('category', fields[i].display_value);
	}
}

debendudas
Mega Sage

Use the below script:

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    var itemNumber = g_form.getValue('item_id');

    var gaFields = new GlideAjax('AssetIntegrationAjaxUtils');
    gaFields.addParam('sysparm_name', 'retrieveAssetData');
    gaFields.addParam('sysparm_id', itemNumber);
    gaFields.getXML(showAssetData);

    function showAssetData(response) {

        var answer = response.responseXML.documentElement.getAttribute("answer");
        alert(answer); //Confirmed that alert is working fine.

        var obj = JSON.parse(answer); // Parsing
        
        //--------UPDATED SCRIPT START-------------
        for(var i in obj.fields){
            g_form.setValue(obj.fields[i].name, obj.fields[i].display_value);
        }
        //--------UPDATED SCRIPT END-------------
    }

}

 

 

If this solution helps you then, mark it as accepted solution ‌‌✔️ and give thumbs up 👍

Amit Verma
Kilo Patron
Kilo Patron

Hi @tsoct 

 

Your JSON string contains an element "fields" under which you have nested elements for Item Name, type and category. To parse it simply, you need to make use of below logic. Please note that this logic will only work if your JSON element position doesn't changes i.e. the JSON structure remains intact. If the JSON structure is dynamic, make use of the solution provided by @Brad Bowman .

 

var parsedJson = JSON.parse(jsonString); // Assuming jsonstring is the variable holding your json
var itemNumber = parsedJson.fields[0].display_value;
var type = parsedJson.fields[2].display_value;
var category = parsedJson.fields[3].display_value;
//Now you can set your catalog item variable values with these variables

 

Thanks and Regards

Amit Verma


Please mark this response as correct and helpful if it assisted you with your question.