NV ToolkitFormat · Validate · Convert

Code Generators

JSON to Pike Converter

Paste a JSON or XML sample — the format is auto-detected — and get typed Pike models with names, nesting and nullability inferred for you.

In short

Convert JSON to Pike online: paste a sample and generate typed Pike models with serialization helpers — powered by quicktype, in your browser.

By NaveenKumar T · Updated

Learn: JSON Examples: From Hello World to Real API Payloads

  • JSON or XML input, auto-detected
  • Nested objects become types
  • Nullability inferred

Runs entirely in your browser — nothing you paste or open here is uploaded, logged, or stored.How we handle data →

Example

Example: JSON order payload to Pike

A small order document with a nested object, an array of two items, a decimal total and a null field — enough to show how each Pike type, list and nullable is inferred. The same page accepts the XML form of this order too, so the sample below is an example rather than the only accepted format. The right-hand side is the exact output of this tool.

Input · JSON
{
  "id": "ord_9F2KX",
  "status": "processing",
  "paid": true,
  "customer": { "name": "Ada Lovelace", "email": "ada@example.com", "phone": null },
  "items": [
    { "sku": "KB-75", "qty": 1, "price": 149.99 },
    { "sku": "MS-12", "qty": 2, "price": 39.5 }
  ],
  "total": 228.99
}
Generated · Pike
// This source has been automatically generated by quicktype.
// ( https://github.com/quicktype/quicktype )
//
// To use this code, simply import it into your project as a Pike module.
// To JSON-encode your object, you can pass it to `Standards.JSON.encode`
// or call `encode_json` on it.
//
// To decode a JSON string, first pass it to `Standards.JSON.decode`,
// and then pass the result to `<YourClass>_from_JSON`.
// It will return an instance of <YourClass>.
// Bear in mind that these functions have unexpected behavior,
// and will likely throw an error, if the JSON string does not
// match the expected interface, even if the JSON itself is valid.

class Root {
    string      id;       // json: "id"
    string      status;   // json: "status"
    bool        paid;     // json: "paid"
    Customer    customer; // json: "customer"
    array(Item) items;    // json: "items"
    float       total;    // json: "total"

    string encode_json() {
        mapping(string:mixed) json = ([
            "id" : id,
            "status" : status,
            "paid" : paid,
            "customer" : customer,
            "items" : items,
            "total" : total,
        ]);

        return Standards.JSON.encode(json);
    }
}

Root Root_from_JSON(mixed json) {
    Root retval = Root();

    retval.id = json["id"];
    retval.status = json["status"];
    retval.paid = json["paid"];
    retval.customer = json["customer"];
    retval.items = json["items"];
    retval.total = json["total"];

    return retval;
}

class Customer {
    string name;  // json: "name"
    string email; // json: "email"
    mixed  phone; // json: "phone"

    string encode_json() {
        mapping(string:mixed) json = ([
            "name" : name,
            "email" : email,
            "phone" : phone,
        ]);

        return Standards.JSON.encode(json);
    }
}

Customer Customer_from_JSON(mixed json) {
    Customer retval = Customer();

    retval.name = json["name"];
    retval.email = json["email"];
    retval.phone = json["phone"];

    return retval;
}

class Item {
    string sku;   // json: "sku"
    int    qty;   // json: "qty"
    float  price; // json: "price"

    string encode_json() {
        mapping(string:mixed) json = ([
            "sku" : sku,
            "qty" : qty,
            "price" : price,
        ]);

        return Standards.JSON.encode(json);
    }
}

Item Item_from_JSON(mixed json) {
    Item retval = Item();

    retval.sku = json["sku"];
    retval.qty = json["qty"];
    retval.price = json["price"];

    return retval;
}

Look for the "phone" field: sampled as null, it is typed as mixed. "items" became array(Item), and "total" is float.

Learn more

From JSON sample to Pike types

Hand-writing model classes from a JSON payload is transcription work: read a field, type a property, repeat eighty times, miss one. This page runs quicktype (the same engine behind the best-known generators) against your document, merging array elements to find the common shape and naming nested types from their keys.

The generated decoders assign raw values straight into a field without checking types, so a payload that drifts produces an object with the wrong type in a field rather than an error. Write one test that decodes a saved production payload and asserts the field types before trusting the classes anywhere else in a running system.

How JSON values become Pike types

Inference works from the values in your sample. Strings map to string, integers and decimals are told apart (228.99 becomes float, while 1 and 2 stay integral), true/false become the language's boolean, and an object such as "customer" becomes its own named type. The "items" array is typed as array(Item) because every element shares the same keys — quicktype unifies the shapes of all array elements into one Item type.

Nulls are the one place inference needs your help: "phone": null carries no type information, so the field is emitted as mixed. If the real API sometimes sends a string there, include a sample where the field is populated (paste several documents merged into one array) and the generated type becomes the union of what it saw.

Pasting XML instead: how elements and attributes become Pike types

The XML is parsed into a data model before inference (attributes included, text nodes named "text"), so the root element becomes a nested type: <order> is a field of type Order on the top-level Root rather than the top level itself. Child elements become fields, and repeated elements — the two <item> siblings — are collected into array(Item) under an Items wrapper type that mirrors the <items> container.

Attributes and element text are typed differently, and this matters: element text is type-inferred, so <total>228.99</total> becomes float, while attribute values are kept as they were written — qty="1" and price="149.99" stay string in Pike. Prefer element text for numeric data you want typed, or cast attribute values after decoding. One more trap: a single <item> would not be recognized as a list, so make sure repeated elements appear at least twice in the sample.

Annotations, nulls and nested arrays in the generated Pike

Pike gets no annotations; the wire name is preserved as a trailing comment on each field — string id; // json: "id" — which keeps the mapping readable even though nothing enforces it. Each class implements encode_json, building a mapping from key to value and handing it to Standards.JSON.encode. Decoding is a free function per class named after it, so the flow is Standards.JSON.decode first and the generated function second. The header comment is candid that malformed input throws rather than reporting cleanly.

A field sampled only as null is typed mixed, Pike's any-type. Merge a document where the field carries a value and the declaration becomes a union such as mixed|string, which at least documents what to expect. Nothing validates it either way: the generated decoder assigns the raw value straight into the field, so a payload that drifts produces a wrongly typed object rather than an error. Treat these declarations as the contract you check yourself. An empty array is typed array(mixed).

Nested objects become their own classes named from the key — Customer, Item — each with its own _from_JSON function, and lists are typed array(Item). One caveat matters more than the rest: the generated decoders are flat. Root's decoder assigns the raw mapping into the field declared as Customer, and the raw array into the field declared as array(Item), so nothing below the top level is really converted. Call the nested decoders yourself, mapping over arrays, if you want typed objects all the way down.

Help

Frequently asked questions

Everything you need to know about the JSON to Pike.

01How do I generate Pike classes from JSON?

Paste a representative document — JSON or XML, the format is detected automatically — and press Convert. A real API response works best. Nested objects become their own Pike types, arrays become array(Item), and the result downloads as Root.pike.

02What does the generated Pike code include?

Pike generation produces typed classes with decode helpers for this venerable C-like scripting language — niche, but when you're in Pike, no other generator has your back.

03How do I use the generated Pike code in my project?

Import the file as a Pike module. Decode with Root_from_JSON(Standards.JSON.decode(text)) and encode by passing the object to Standards.JSON.encode — each class implements encode_json. The helpers raise an error when the decoded JSON does not match the declared fields.

04Pike? Really?

Really — Pike powers some long-lived systems (it grew out of LPC/MUD heritage and backs Roxen). If you maintain one, mechanical model generation beats hand-mapping mappings every time.

Keep working

Related tools

Learn the format

Related guides

All guides →