Custom script examples (general)

Introduction

This page includes scripts that may be useful either in and of themselves or as a starting point for your own scripting work:

C#

This script takes an XML input string, loads it into an XmlDocument, and then converts that XML to a JSON string using the Newtonsoft.Json library.

Converting XML to JSON
using System;
using System.Xml;
using Newtonsoft.Json;
public class FunctionHandler
{
    public string Handle(string input)
    {
        var xmlDoc = new XmlDocument();
        
        // Set up XML namespace manager
        var namespaceManager = new XmlNamespaceManager(xmlDoc.NameTable);
        namespaceManager.AddNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
        namespaceManager.AddNamespace("", "http://api.jdplc.com/schemas/mc/pim/feproducts");
        
        // Load the XML string into XmlDocument
        xmlDoc.LoadXml(input);

        // Convert the XML to JSON
        string jsonText = JsonConvert.SerializeXmlNode(xmlDoc, Newtonsoft.Json.Formatting.Indented, true);

        return jsonText;
    }
}

Go

The script below demonstrates the handling of variables, logging, updating response codes, and modifying payloads.

Helper functions demo

JavaScript

The script below demonstrates how to convert a JSON file to XML, using JavaScript.

JSON to XML

PHP

Converting JSON to CSV

Python

The script below demonstrates how to convert a CSV file to JSON, using Python.

CSV to JSON

Rust

The script below demonstrates how helper functions can be used, with the handle() function acting as the main driver.

Helper functions demo

Last updated

Was this helpful?