JSON to Code

JSON to C++

Paste a JSON sample. Get C++ 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 C++ 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 C++:

#pragma once

#include <optional>
#include <stdexcept>
#include <regex>

class Address {
    public:
    Address() = default;
    virtual ~Address() = default;

    private:
    std::string city;
    nlohmann::json postcode;

    public:
    const std::string & get_city() const { return city; }
    std::string & get_mutable_city() { return city; }
    void set_city(const std::string & value) { this->city = value; }

    const nlohmann::json & get_postcode() const { return postcode; }
    nlohmann::json & get_mutable_postcode() { return postcode; }
    void set_postcode(const nlohmann::json & value) { this->postcode = value; }
};

class Root {
    public:
    Root() = default;
    virtual ~Root() = default;

    private:
    int64_t id;
    std::string name;
    bool active;
    std::string joined;
    std::vector<std::string> tags;
    Address address;
    std::vector<double> scores;

    public:
    const int64_t & get_id() const { return id; }
    int64_t & get_mutable_id() { return id; }
    void set_id(const int64_t & value) { this->id = value; }

    const std::string & get_name() const { return name; }
    std::string & get_mutable_name() { return name; }
    void set_name(const std::string & value) { this->name = value; }

    const bool & get_active() const { return active; }
    bool & get_mutable_active() { return active; }

and 19 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.

//  To parse this JSON data, first install
//
//      json.hpp  https://github.com/nlohmann/json
//
//  Then include this file, and then do
//
//     Root data = nlohmann::json::parse(jsonString);

#pragma once

#include "json.hpp"

#include <optional>
#include <stdexcept>
#include <regex>

namespace quicktype {
    using nlohmann::json;

    #ifndef NLOHMANN_UNTYPED_quicktype_HELPER
    #define NLOHMANN_UNTYPED_quicktype_HELPER
    inline json get_untyped(const json & j, const char * property) {
        if (j.find(property) != j.end()) {
            return j.at(property).get<json>();
        }
        return json();
    }

    inline json get_untyped(const json & j, std::string property) {
        return get_untyped(j, property.data());
    }
    #endif

    class Address {
        public:
        Address() = default;
        virtual ~Address() = default;

        private:
        std::string city;
        nlohmann::json postcode;

        public:
        const std::string & get_city() const { return city; }
        std::string & get_mutable_city() { return city; }

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

What you need in your project

nlohmann/json.

The output uses the header-only nlohmann library, which is the one most C++ projects pick for JSON.

The file holds a struct for each object and the `from_json` and `to_json` functions the library looks for.

The trap to know about

A missing key throws. Give the field a default, or check with `contains` before you read it.

The options you can change

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

OptionWhat it doesDefault
Code formatGenerate classes with getters/setters, instead of structswith-struct | with-getter-setter
Store strings using Utf-16 std::wstring, rather than Utf-8 std::stringStore strings using Utf-16 std::wstring, rather than Utf-8 std::stringuse-string | use-wstring
const stylePut const to the left/west (const T) or right/east (T const)west-const | east-const
Types onlyPlain types onlyfalse
NamespaceName of the generated namespace(s)quicktype
Type namingNaming style for typespascal-case | original-case | underscore-case | camel-case | upper-underscore-case | pascal-case-upper-acronyms | camel-case-upper-acronyms
Member namingNaming style for memberspascal-case | original-case | underscore-case | camel-case | upper-underscore-case | pascal-case-upper-acronyms | camel-case-upper-acronyms
Enum namingNaming style for enumeratorspascal-case | original-case | underscore-case | camel-case | upper-underscore-case | pascal-case-upper-acronyms | camel-case-upper-acronyms

Questions

How do I turn JSON into C++?

Paste your JSON into the box, choose C++, 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 C++ project?

nlohmann/json.

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 C, with cJSON JSON to Crystal 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