CodeStageGen

Parse JSON in a Blue Prism Code Stage (no third-party DLL)

Updated 25 September 2026 · Every C# sample here compiles with the compiler that ships with Blue Prism.

APIs, web services and many modern applications return JSON, and sooner or later a Blue Prism process has to read it. Many people reach for Newtonsoft.Json straight away. It does ship with Blue Prism, but for most jobs you don't need any third-party library. .NET has had a JSON parser built in for years: JavaScriptSerializer.

Setup: one reference, one import

In Code Options on the object's Initialise page, add:

  • External References: System.Web.Extensions.dll
  • Namespace Imports: System.Web.Script.Serialization

Both are part of the .NET Framework on every Windows machine, so there's nothing to copy to your robots.

If you miss one, the errors are:

  • No reference: The type or namespace name 'Script' does not exist in the namespace 'System.Web' (are you missing an assembly reference?)
  • No import: The type or namespace name 'JavaScriptSerializer' could not be found

Read a value from a JSON object

Input Json Text (Text). Outputs Customer Name (Text) and Total (Number). For this input:

Json
{ "customer": { "name": "Ada Lovelace", "id": 42 }, "total": 12.5 }
C#
var serializer = new JavaScriptSerializer();
var root = (Dictionary<string, object>)serializer.DeserializeObject(Json_Text);
var customer = (Dictionary<string, object>)root["customer"];
Customer_Name = Convert.ToString(customer["name"]);
Total = Convert.ToDecimal(root["total"]);

DeserializeObject returns plain .NET types:

JSON.NET type you get
{ ... } objectDictionary<string, object>
[ ... ] arrayobject[]
"text"string
42int (or long for large values)
12.5decimal
true / falsebool
nullnull

Cast to the type you expect, and convert numbers with Convert.ToDecimal before assigning them to a Blue Prism Number, which is a decimal.

Optional fields

root["customer"] throws KeyNotFoundException when the key is missing. For fields that may not be there, check first:

C#
object value;
Customer_Name = root.TryGetValue("customer_name", out value) && value != null
    ? Convert.ToString(value)
    : "";

Turn a JSON array into a collection

This is the one people need most: an API returns a list of records, and you want a Blue Prism collection. Input Json Text (Text), output Output Collection (Collection). Add System.Globalization to Namespace Imports too.

C#
var serializer = new JavaScriptSerializer();
serializer.MaxJsonLength = int.MaxValue;
object[] items = (object[])serializer.DeserializeObject(Json_Text);
DataTable table = new DataTable();
foreach (object item in items)
{
    var fields = (Dictionary<string, object>)item;
    foreach (string key in fields.Keys)
        if (!table.Columns.Contains(key))
            table.Columns.Add(key, typeof(string));

    DataRow row = table.NewRow();
    foreach (var field in fields)
    {
        object value = field.Value;
        if (value == null)
            row[field.Key] = "";
        else if (value is Dictionary<string, object> || value is object[])
            row[field.Key] = serializer.Serialize(value);   // keep nested JSON as text
        else
            row[field.Key] = Convert.ToString(value, CultureInfo.InvariantCulture);
    }
    table.Rows.Add(row);
}
Output_Collection = table;

What it handles:

  • Records with different fields. Columns are added as new keys appear. A record without a field gets an empty cell.
  • Nested objects and arrays stay as JSON text in their cell, so no data is lost. Parse that cell again in a later stage if you need it.
  • Numbers are written with the invariant culture, so 10.75 never becomes 10,75 on a robot with European regional settings.
  • Large payloads. MaxJsonLength defaults to 2,097,152 characters. Big API responses exceed that and throw, so the example raises the limit.

Every column is Text. If you need Number columns, convert them in a later stage, or add the columns with typeof(decimal) and convert values with Convert.ToDecimal(value, CultureInfo.InvariantCulture).

Build JSON to send

Serialize goes the other way. Build a dictionary and serialize it:

C#
var serializer = new JavaScriptSerializer();
var body = new Dictionary<string, object>
{
    { "invoiceNumber", Invoice_Number },
    { "amount", Amount },
    { "approved", true }
};
Json_Text = serializer.Serialize(body);

Don't build JSON by joining strings together. A quote or backslash in the data produces invalid JSON, and Serialize escapes them for you.

When Newtonsoft.Json is worth it

JavaScriptSerializer covers most robot work: reading fields, flattening arrays, building request bodies. Newtonsoft.Json (which ships with Blue Prism) is worth using for JSONPath queries (SelectToken("$.orders[0].id")) or deeply nested documents of unknown shape. Remember that a referenced DLL has to be loadable on every machine that runs the process.

Related

More Code Stage guides