JSON to Code

JSON to Pike

Paste a JSON sample. Get Pike types, and the code that reads and writes the JSON. It runs in your browser, and your data never leaves it.

Open the tool with Pike chosen

An example

This JSON:

{
  "id": 42,
  "name": "Ada Lovelace",
  "active": true,
  "joined": "2020-01-02T03:04:05Z",
  "tags": ["engineer", "writer"],
  "address": { "city": "London", "postcode": null },
  "scores": [9.5, 8, 10]
}

becomes this 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 {
    int           id;      // json: "id"
    string        name;    // json: "name"
    bool          active;  // json: "active"
    string        joined;  // json: "joined"
    array(string) tags;    // json: "tags"
    Address       address; // json: "address"
    array(float)  scores;  // json: "scores"

    string encode_json() {
        mapping(string:mixed) json = ([
            "id" : id,
            "name" : name,
            "active" : active,
            "joined" : joined,
            "tags" : tags,
            "address" : address,
            "scores" : scores,
        ]);

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

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

    retval.id = json["id"];
    retval.name = json["name"];
    retval.active = json["active"];
    retval.joined = json["joined"];
    retval.tags = json["tags"];
    retval.address = json["address"];
    retval.scores = json["scores"];

    return retval;

and 25 more lines. Open the tool to see the whole file.

With the reader and the writer

The tool also writes the code that turns the text into those types, and back again. That code checks the shape as it reads, which is the part a type alone cannot do.

// 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 {
    int           id;      // json: "id"
    string        name;    // json: "name"
    bool          active;  // json: "active"
    string        joined;  // json: "joined"
    array(string) tags;    // json: "tags"
    Address       address; // json: "address"
    array(float)  scores;  // json: "scores"

    string encode_json() {
        mapping(string:mixed) json = ([
            "id" : id,
            "name" : name,
            "active" : active,
            "joined" : joined,
            "tags" : tags,
            "address" : address,
            "scores" : scores,
        ]);

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

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

    retval.id = json["id"];
    retval.name = json["name"];
    retval.active = json["active"];
    retval.joined = json["joined"];

and 30 more lines. Open the tool to see the whole file.

What you need in your project

Nothing. `Standards.JSON` is in the standard library.

A class for each object, in the Pike style.

The trap to know about

Pike is rare enough that few examples exist. Read the generated reader before you trust it with a strange field name.

The options you can change

These belong to Pike, so the row of switches changes with the language.

OptionWhat it doesDefault

Questions

How do I turn JSON into Pike?

Paste your JSON into the box, choose Pike, and the code appears at once. Give the top type a name, and every other name follows from your keys. Then copy the code, or download it as a file.

Does my JSON go to a server?

No. The generator runs inside your browser, in a worker. Nothing is uploaded, nothing is stored, and the tool works with the network turned off.

What do I need to add to my Pike project?

Nothing. `Standards.JSON` is in the standard library.

How does it know which fields are optional?

From your sample. A key that is missing, or holds null, becomes optional. One sample cannot show everything, so give more than one, or start from a JSON Schema.

Other languages

JSON to Smithy All the guides

Try it on your own JSON. The generator runs in your browser. Nothing is uploaded, and nothing is stored on a server.

Open JSON to Code