Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How I can do JSON file import and YAML import in ServiceNow

keshav77
Tera Contributor

Hi All,

 

I am  working on catalog where  I need to give user access to attach the attachment where the attachment can be JSON and YAML. How to parse these 2 file and upload the data in the target table

2 REPLIES 2

Sarthak Kashyap
Mega Sage

Hi @keshav77 ,

 

Can you please try to create a Business rule on Before on RITM table when insert and add below code

 

(function() {
    var attachment = new GlideRecord('sys_attachment');
    attachment.addQuery('table_sys_id', current.sys_id);
    attachment.query();

    while (attachment.next()) {
        var sysAtt = new GlideSysAttachment();
        var content = sysAtt.getContent(attachment);

        var fileName = attachment.file_name.toString();
        var data = null;

        if (fileName.endsWith('.json')) {
            data = JSON.parse(content);
        }

        if (fileName.endsWith('.yaml') || fileName.endsWith('.yml')) {
            var yaml = new sn_impex.GlideYAMLParser();
            data = yaml.parse(content);
        }

        if (data) {
            var target = new GlideRecord('u_target_table');
            target.initialize();
            target.u_name = data.name;
            target.insert();
        }
    }
})();

 

Please mark my answer correct and helpful if this works for you

Thanks and Regards,

Sarthak

keshav77
Tera Contributor

@Ankur Bawiskar  any suggestion this !!