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.

chevron-right Converting XML to JSONhashtag
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.

chevron-right Helper functions demohashtag

JavaScript

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

chevron-right JSON to XMLhashtag

PHP

chevron-right Converting JSON to CSVhashtag

Python

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

chevron-right CSV to JSONhashtag

Rust

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

chevron-right Helper functions demohashtag

Last updated

Was this helpful?