Parse JSON and create record

Rj27
Mega Guru

Hi,
i want to create new records in table when the api is triggered. My payload is of following format:

 

 

 

{
"TaskNum":"1",
"TaskName":"ABC",
"Link":"https://www.google.com/",
    "Data1":{
    "Type":"T1",
    "Priority":1,
    "Date":"4-11-2022"
    },
    "Data2":{
    "Type":"T2",
    "Priority":1,
    "Date":"4-11-2022"
    },
}

 

 

or payload can be like this :

 

 

{
{
"TaskNum":"1",
"TaskName":"ABC",
"Link":"https://www.google.com/",
    "Data1":{
    "Type":"T1",
    "Priority":1,
    "Date":"4-11-2022"
    },
   "Data2":{
    "Type":"T2",
    "Priority":1,
    "Date":"4-11-2022"
    }
},
{
"TaskNum":"2",
"TaskName":"XYZ",
"Link":"https://www.gmail.com/",
    "Data1":{
    "Type":"T1",
    "Priority":1,
    "Date":"4-11-2022"
    },
}
}

 

 

 

 

Number of records that will be created in table will depend on the payload values.
For example:
1. If first payload triggered, in this case 2 records( 1 for data 1 and 1 for data 2) will be created for this number (TaskNum 1).
Basically in this case number of records will depend on the number of datas.

2. If second payload is triggered, 3 records will be created(2 records(based on number of datas) for TaskNum 1 and 1 record for TaskNum 2)

The custom table has fields where TaskNum and Taskname values will be mapped to.
**There will be atleast 1 TaskNum with atleast 1 data. That is there should be atleast 1 record created.

How to parse this and use it in script ?

 

 

2 REPLIES 2

MD AQUIB KHAN
Giga Guru
Hi Rj,
As per your  1st Payload
I have tried to stringify and parse the payload  using fix script so that you can have an idea
var payload = {
    "TaskNum": "1",	
    "TaskName": "ABC",
    "Link": "https://www.google.com/",
    "Data1": {
        "Type": "T1",
        "Priority": 1,
        "Date": "4-11-2022"
    },
    "Data2": {
        "Type": "T2",
        "Priority": 1,
        "Date": "4-11-2022"
    }
};
var grjson = JSON.stringify(payload);
gs.print('JSON :' +grjson);
var parser = JSON.parse(grjson);
gs.print("Parsed Task Num:" + parser.TaskNum);
gs.print("Parsed Task Name:" + parser.TaskName);
gs.print("Parsed Data1:" + parser.Data1.Type + "Priority :" +parser.Data1.Priority + "Date1" + parser.Data1.Date);
gs.print("Parsed Data2:" + parser.Data2.Type + "Priority :" +parser.Data2.Priority + "Date2" + parser.Data2.Date);

// Now coming to your queries , The second payload is showing as duplicate entry . I will attached the screenshot . You can have look. Please share sample payload . Not sure if you have share the exact payload.
If you are triggering through the integration . You can get the entire payload in the one variable.
something like 
var jsonarr = [];
jsonarr.push(grjson);
gs.print('multirequest' +jsonarr ); // Try to validate once .
Question is how  you will utilising in your requestbody . Sample script.
var r = new sn_ws.RESTMessageV2('Integration using  SCripted Rest API', 'Update Incident'); // just a sample name
 r.setStringParameterNoEscape('multirequest', jsonarr.toString());
// Similarly you can send the other attribute as well .
  var response = r.execute();
 var responseBody = response.getBody();	   //get Response Body        		
 var httpStatus = response.getStatusCode(); 
 var getBody = r.getRequestBody();
NOTE: This is not the exact solution but just giving you the fair idea to get started . I will be sharing few links to you show that it can helpfull. 

Duplicate Key.PNG

 

Important Link: JSON Parsing - ServiceNow Community

                          Solved: Get values ​​from an array in Json format - ServiceNow Community

Hello Aquib,
I was able to parse the data but i am stuck at getting count of each datas since they are not part of any array in payload. My payload can have 'n' number of datas and total records that will be created in table will depend on this n.
For eg; In ths case 2 records will be created in my table because there are two datas..

{
    "TaskNum": "1",	
    "TaskName": "ABC",
    "Link": "https://www.google.com/",
    "Data1": {
        "Type": "T1",
        "Priority": 1,
        "Date": "4-11-2022"
    },
    "Data2": {
        "Type": "T2",
        "Priority": 1,
        "Date": "4-11-2022"
    }
}

 

My table has few columns(number, name, data) where the values from payload will be mapped.