counting records from a JSON

deepika46
Tera Contributor

Hello Experts,

 

I have a json file. Is there any script i can use in powershell to get the count of the records from it

 

Consider an example below:

{
"records": [
{
"sys_id": "000018ffsada",
"sys_created_on": "02-25-2020 07:53:56",
"name": "sc_req_item",
"element_id": "5c3707dasd",
"sys_tags": "",
"value": "xyz'",
"sys_created_by": "abc",
"element": "work_notes",
"__status": "success"
},
{
"sys_id": "000030031bb6809das",
"sys_created_on": "02-10-2020 16:02:37",
"name": "sc_req_item",
"element_id": "7228a4871bf6cc5ww",
"sys_tags": "",
"value": "hello'",
"sys_created_by": "sss",
"element": "work_notes",
"__status": "success"
},

}

Like there are 2 records which is the count. Similarly i have a file consisting of lakhs of data. Is there any way i can get the count of records from the file.

 

1 ACCEPTED SOLUTION

Its_Azar
Tera Guru

Hi there @deepika46 

 

you can use scripting  using a script include:

var JSONParser = Class.create();
JSONParser.prototype = {
    initialize: function() {},

    countRecords: function(jsonPayload) {
        var recordCount = 0;

    
        if (jsonPayload && jsonPayload.records && Array.isArray(jsonPayload.records)) {
           
            recordCount = jsonPayload.records.length;
        }

        return recordCount;
    }
};

Now, you can use this script include in any server-side script or business rule to count records in a JSON payload:

var jsonPayload = {
    "records": [
        {
            "sys_id": "000018ffsada",
            "sys_created_on": "02-25-2020 07:53:56",
            "name": "sc_req_item",
            "element_id": "5c3707dasd",
            "sys_tags": "",
            "value": "xyz'",
            "sys_created_by": "abc",
            "element": "work_notes",
            "__status": "success"
        },
        {
            "sys_id": "000030031bb6809das",
            "sys_created_on": "02-10-2020 16:02:37",
            "name": "sc_req_item",
            "element_id": "7228a4871bf6cc5ww",
            "sys_tags": "",
            "value": "hello'",
            "sys_created_by": "sss",
            "element": "work_notes",
            "__status": "success"
        }
    ]
};


var parser = new JSONParser();


var recordCount = parser.countRecords(jsonPayload);
gs.info("Number of records: " + recordCount);

 If this helps kindly accept the response thanks much.

☑️ If this helped, please mark it as Helpful or Accept Solution so others can find the answer too.




Kind Regards,

Mohamed Azarudeen Z

Developer @ KPMG

 Microsoft MVP (AI Services), India

View solution in original post

1 REPLY 1

Its_Azar
Tera Guru

Hi there @deepika46 

 

you can use scripting  using a script include:

var JSONParser = Class.create();
JSONParser.prototype = {
    initialize: function() {},

    countRecords: function(jsonPayload) {
        var recordCount = 0;

    
        if (jsonPayload && jsonPayload.records && Array.isArray(jsonPayload.records)) {
           
            recordCount = jsonPayload.records.length;
        }

        return recordCount;
    }
};

Now, you can use this script include in any server-side script or business rule to count records in a JSON payload:

var jsonPayload = {
    "records": [
        {
            "sys_id": "000018ffsada",
            "sys_created_on": "02-25-2020 07:53:56",
            "name": "sc_req_item",
            "element_id": "5c3707dasd",
            "sys_tags": "",
            "value": "xyz'",
            "sys_created_by": "abc",
            "element": "work_notes",
            "__status": "success"
        },
        {
            "sys_id": "000030031bb6809das",
            "sys_created_on": "02-10-2020 16:02:37",
            "name": "sc_req_item",
            "element_id": "7228a4871bf6cc5ww",
            "sys_tags": "",
            "value": "hello'",
            "sys_created_by": "sss",
            "element": "work_notes",
            "__status": "success"
        }
    ]
};


var parser = new JSONParser();


var recordCount = parser.countRecords(jsonPayload);
gs.info("Number of records: " + recordCount);

 If this helps kindly accept the response thanks much.

☑️ If this helped, please mark it as Helpful or Accept Solution so others can find the answer too.




Kind Regards,

Mohamed Azarudeen Z

Developer @ KPMG

 Microsoft MVP (AI Services), India