json-build is a zero-allocation JSON serializer compatible with C89



json-build is a zero-allocation JSON serializer compatible with C89. It is inspired by jsmn, a minimalistic JSON tokenizer.


  • compatible with C89
  • no dependencies
  • no dynamic memory allocation


Download json-build.h, include it, done.

#include "json-build.h"

jsonb b;
char buf[1024];

jsonb_push_object(&b, buf, sizeof(buf));
    jsonb_push_key(&b, buf, sizeof(buf), "foo", strlen("foo"));
    jsonb_push_array(&b, buf, sizeof(buf));
        jsonb_push_number(&b, buf, sizeof(buf), 1);
        jsonb_push_string(&b, buf, sizeof(buf), "hi", 2);
        jsonb_push_bool(&b, buf, sizeof(buf), 0);
        jsonb_push_null(&b, buf, sizeof(buf));
        jsonb_pop_array(&b, buf, sizeof(buf));
    jsonb_pop_object(&b, buf, sizeof(buf));
printf("JSON: %s", buf); // JSON: {"foo":[1,"hi",false,null]}

Since json-build is a single-header, header-only library, for more complex use cases you might need to define additional macros. #define JSONB_STATIChides all json-build API symbols by making them static. Also, if you want to include json-build.h for multiple C files, to avoid duplication of symbols you may define JSONB_HEADER macro.

/* In every .c file that uses json-build include only declarations: */
#include "json-build.h"

/* Additionally, create one json-build.c file for json-build implementation: */
#include "json-build.h"


  • jsonb_init() - initialize a jsonb handle
  • jsonb_push_object() - push an object to the builder stack
  • jsonb_pop_object() - pop an object from the builder stack
  • jsonb_push_key() - push an object key field to the builder stack
  • jsonb_push_array() - push an array to the builder stack
  • jsonb_pop_array() - pop an array from the builder stack
  • jsonb_push_token() - push a raw token to the builder stack
  • jsonb_push_bool() - push a boolean token to the builder stack
  • jsonb_push_null() - push a null token to the builder stack
  • jsonb_push_string() - push a string token to the builder stack
  • jsonb_push_number() - push a number token to the builder stack

The following are the possible return codes for the builder functions:

  • JSONB_OK - operation was a success, user can proceed with the next operation
  • JSONB_END - operation was a success, JSON is complete and expects no more operations
  • JSONB_ERROR_NOMEM - buffer is not large enough
  • JSONB_ERROR_INPUT - user action don't match expected next token
  • JSONB_ERROR_STACK - user action would lead to out of boundaries access, increase JSONB_MAX_DEPTH!

Its worth mentioning that all JSONB_ERROR_ prefixed codes are negative.

If you get JSONB_ERROR_NOMEM you can re-allocate a larger buffer and call the builder function once more.

Other info

This software is distributed under MIT license, so feel free to integrate it in your commercial products.

You might also like...
C library for encoding, decoding and manipulating JSON data

Jansson README Jansson is a C library for encoding, decoding and manipulating JSON data. Its main features and design principles are: Simple and intui

JSON & BSON parser/writer

jbson is a library for building & iterating BSON data, and JSON documents in C++14. \tableofcontents Features # {#features} Header only. Boost license

A very sane (header only) C++14 JSON library

JeayeSON - a very sane C++14 JSON library JeayeSON was designed out of frustration that there aren't many template-based approaches to handling JSON i

Jsmn is a world fastest JSON parser/tokenizer. This is the official repo replacing the old one at Bitbucket

JSMN jsmn (pronounced like 'jasmine') is a minimalistic JSON parser in C. It can be easily integrated into resource-limited or embedded projects. You

JSON for Modern C++
JSON for Modern C++

Design goals Sponsors Integration CMake Package Managers Pkg-config Examples JSON as first-class data type Serialization / Deserialization STL-like ac

A JSON parser in C++

JSON++ Introduction JSON++ is a light-weight JSON parser, writer and reader written in C++. JSON++ can also convert JSON documents into lossless XML d

🗄️ single header json parser for C and C++

🗄️ json.h A simple single header solution to parsing JSON in C and C++. JSON is parsed into a read-only, single allocation buffer. The current suppor

A C++ library for interacting with JSON.

JsonCpp JSON is a lightweight data-interchange format. It can represent numbers, strings, ordered sequences of values, and collections of name/value p

Very low footprint JSON parser written in portable ANSI C

Very low footprint JSON parser written in portable ANSI C. BSD licensed with no dependencies (i.e. just drop the C file into your project) Never recur

  • question about jsonb_init()

    question about jsonb_init()


    I could not find a better way to ask a question about some implementation details in jsonb_init(), from jason-build. What is the purpose of the following code: void jsonb_init(jsonb *b) { static jsonb empty_builder; *b = empty_builder; ....

    Thanks in advance. Volodimir

    opened by volodimirsmartwave 9
  • possible memory leak when calling _jsonb_escape

    possible memory leak when calling _jsonb_escape


    We are using your amazing library and found a potential mem leak. I'm not sure the patch we prepared to solve it is good though:


    Seems like _jsonb_escape was using the complete bufsize but buf was passed as buf + b->pos. So could _jsonb_escape be writing beyond the limits of buf?

    To give some context, we are allocating an initial buffer that we resize if any of the build functions return JSONB_ERROR_NOMEM.

    opened by anibalportero 2
  • v1.1.1(Sep 3, 2022)

  • v1.1.0(Jun 16, 2022)

  • v1.0.1(Apr 15, 2022)

    What's Changed

    • docs: better document JSONB_MAX_DEPTH by @lcsmuller in https://github.com/lcsmuller/json-build/pull/2

    New Contributors

    • @lcsmuller made their first contribution in https://github.com/lcsmuller/json-build/pull/2

    Full Changelog: https://github.com/lcsmuller/json-build/compare/v1.0.0...v1.0.1

    Source code(tar.gz)
    Source code(zip)
  • v1.0.0(Feb 5, 2022)

  • v0.0.0(Feb 5, 2022)

Lucas Müller
Lucas Müller
A C++, header-only library for constructing JSON and JSON-like data formats, with JSON Pointer, JSON Patch, JSON Schema, JSONPath, JMESPath, CSV, MessagePack, CBOR, BSON, UBJSON

JSONCONS jsoncons is a C++, header-only library for constructing JSON and JSON-like data formats such as CBOR. For each supported data format, it enab

Daniel Parker 547 Jan 4, 2023
a header-file-only, JSON parser serializer in C++

PicoJSON - a C++ JSON parser / serializer Copyright © 2009-2010 Cybozu Labs, Inc. Copyright © 2011-2015 Kazuho Oku Licensed under 2-clause BSD license

Kazuho Oku 1k Dec 27, 2022
A generator of JSON parser & serializer C++ code from structure header files

JSON-CPP-gen This is a program that parses C++ structures from a header file and automatically generates C++ code capable of serializing said structur

Viktor Chlumský 12 Oct 13, 2022
Convert YouTube Subscriptions JSON into RSS Reader Compatible OPML

OPMLify Convert YouTube Subscriptions JSON into RSS Reader Compatible OPML Brief Overview OPMLify allows you to import your YouTube Subscriptions to a

null 13 May 25, 2022
Build complex rules, serialize them as JSON, and execute them in C++

json-logic-cpp This parser accepts JsonLogic rules and executes them in C++ programs. The JsonLogic format is designed to allow you to share rules (lo

Gabriel Kim 6 Sep 15, 2022
json-cpp is a C++11 JSON serialization library.

JSON parser and generator for C++ Version 0.1 alpha json-cpp is a C++11 JSON serialization library. Example #include <json-cpp.hpp> struct Foo {

Anatoly Scheglov 7 Oct 30, 2022
This is a JSON C++ library. It can write and read JSON files with ease and speed.

Json Box JSON (JavaScript Object Notation) is a lightweight data-interchange format. Json Box is a C++ library used to read and write JSON with ease a

Anhero inc. 110 Dec 4, 2022
A convenience C++ wrapper library for JSON-Glib providing friendly syntactic sugar for parsing JSON

This library is a wrapper for the json-glib library that aims to provide the user with a trivial alternative API to the API provided by the base json-

Rob J Meijer 17 Oct 19, 2022
Ultralightweight JSON parser in ANSI C

cJSON Ultralightweight JSON parser in ANSI C. Table of contents License Usage Welcome to cJSON Building Copying the source CMake Makefile Vcpkg Includ

Dave Gamble 8.3k Jan 4, 2023
JSON parser and generator for C/C++ with scanf/printf like interface. Targeting embedded systems.

JSON parser and emitter for C/C++ Features ISO C and ISO C++ compliant portable code Very small footprint No dependencies json_scanf() scans a string

Cesanta Software 637 Dec 30, 2022