October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run ScanOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
Binary Formats

From JSON to FlatBuffers: A Practical flatc Workflow

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

To convert JSON into a FlatBuffer binary, provide flatc with a matching FlatBuffers schema and the JSON document:

flatc --binary schema.fbs data.json

This is schema-based serialization, not an automatic conversion of arbitrary JSON. The schema defines tables, types, vectors, enums, defaults and the root object; flatc validates the JSON against that contract and writes a binary buffer.

What the conversion does

JSON is human-readable text, but applications normally parse it and construct objects before using the data. FlatBuffers is a cross-platform, schema-defined binary format designed for direct access to serialized data. It can reduce parsing and allocation work, but whether it is faster or smaller depends on data shape, language runtime, access pattern, compression and whether full object materialization is required. The project describes these design goals in its official repository.

JSON document + schema.fbs + flatc compiler
                         ↓
                  FlatBuffer binary

JSON-to-binary conversion is especially useful for build-time assets, test fixtures, catalogs, configuration packaging and legacy-data imports. For high-throughput runtime ingestion, repeatedly invoking a compiler is usually the wrong architecture; applications commonly build buffers with generated APIs instead.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Minimal working example

1. Define the schema

namespace Example;

enum WeaponType : byte {
  Sword,
  Axe
}

table Weapon {
  name:string;
  damage:short;
}

table Monster {
  pos:[float];
  mana:short = 150;
  hp:short = 100;
  name:string;
  inventory:[ubyte];
  weapons:[Weapon];
  equipped:WeaponType = Sword;
}

root_type Monster;
file_identifier "MONS";
  • namespace controls generated-language namespacing.
  • A table is the usual flexible object type.
  • [ubyte] is a byte vector and [Weapon] is a vector of nested tables.
  • The enum restricts equipped to declared symbolic values.
  • root_type identifies the top-level object.
  • The four-character file_identifier helps identify the intended schema during inspection.

The schema language also supports structs, unions, included schemas, deprecated fields, explicit IDs and attributes; see the schema-writing guide for those features.

2. Supply matching JSON

{
  "pos": [1.0, 2.0, 3.0],
  "mana": 120,
  "hp": 80,
  "name": "Orc",
  "inventory": [1, 2, 3, 4],
  "weapons": [
    {"name": "Sword", "damage": 35},
    {"name": "Axe", "damage": 50}
  ],
  "equipped": "Sword"
}

Names must match exactly, including case. Numbers must fit their declared FlatBuffers types, and enum values are normally written symbolically. Omitted fields use schema defaults or remain absent according to the type and schema rules; omission does not necessarily mean an explicitly stored value.

3. Compile the binary

flatc --binary monster.fbs monster.json

The compiler normally creates a file such as monster_wire.bin. The exact name can vary with options and schema attributes, so inspect the output directory rather than hard-coding a filename.

4. Generate application bindings

flatc --cpp monster.fbs
flatc --rust monster.fbs
flatc --cpp --rust monster.fbs

Current compiler documentation lists generators including C++, Java, Kotlin, C#, Go, Python, JavaScript, TypeScript, PHP, Dart, Lua, Rust, Swift and Nim. Generator features and runtime packaging differ by language; consult the compiler documentation.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

5. Inspect the result

flatc --json monster.fbs -- monster_wire.bin
flatc --json --strict-json monster.fbs -- monster_wire.bin

The -- separator marks following files as binary inputs. --strict-json quotes field names and disallows trailing commas, which is preferable for standard JSON tooling. Round-tripped text is not guaranteed to match the source byte-for-byte: defaults, field order, enum formatting and numeric formatting may differ.

Installing and pinning flatc

Verify the executable independently:

flatc --version

Installing a language runtime library does not necessarily install the flatc compiler. Use a system package, package-manager executable or prebuilt release, or build from source. The repository documents a typical Unix-like CMake build:

cmake -G "Unix Makefiles"
make -j

Pin the compiler version, runtime-library version and schemas in CI. The official releases page showed v25.12.19 and a later commit-tagged release on August 18, 2026; release status changes, so verify the version you install.

Useful flatc commands

Purpose Command
Write binary flatc --binary schema.fbs data.json
Choose output directory flatc --binary -o build/generated schema.fbs data.json
Generate bindings and binary flatc --cpp --binary -o build/generated schema.fbs data.json
Import schemas flatc --binary -I schemas schemas/root.fbs data.json
Binary to JSON flatc --json schema.fbs -- data.bin
Read size-prefixed data flatc --json --size-prefixed schema.fbs -- data.bin
Emit default-valued fields in JSON flatc --binary --defaults-json schema.fbs data.json
Require explicit field IDs flatc --require-explicit-ids --cpp schema.fbs
Check schema conformance flatc --conform old_schema.fbs new_schema.fbs

--defaults-json matters when producing JSON and wanting values equal to schema defaults shown explicitly. Explicit and omitted defaults can be semantically equivalent to readers while remaining different in source and debugging output.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

JSON and schema compatibility rules

Names, types and ranges

FlatBuffers distinguishes byte, ubyte, short, ushort, int, uint, long, ulong, float and double, whereas JSON has one general number syntax. Validate ranges before compilation. Large integers can lose precision before flatc if processed through JavaScript Number; use a typed pipeline when exact 64-bit values matter.

Strings and bytes

FlatBuffers strings are UTF-8-oriented. Options such as --allow-non-utf8 and --natural-utf8 are specialized interoperability controls, not a repair for malformed text. A byte vector is represented textually, for example {"payload":[0,1,2,255]} for payload:[ubyte]; large payloads should be preprocessed rather than expanded into huge JSON arrays.

--json-nested-bytes can interpret nested FlatBuffer data as bytes, but the documentation warns that the result must be verified before use.

Enums, unions and structures

Enum JSON should use declared names such as "Ready". Unknown names fail; renaming a member changes JSON compatibility even if its numeric value remains. A union generally requires both a discriminator and a value, so test every branch. A struct has fixed inline layout and stricter evolution constraints; independently evolving application objects normally belong in tables.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Business-required fields still need validation in preprocessing, generated APIs or application code. A schema declaration alone is not a complete business validation policy.

Validation and round-trip testing

  1. Parse and lint the input with a normal JSON parser.
  2. Compile representative fixtures with flatc; treat diagnostics as contract failures.
  3. Read each binary with the target runtime and use its verifier for untrusted data.
  4. Convert a fixture back with --json --strict-json.
  5. Compare normalized semantic data, not textual identity.

A practical CI sequence is:

flatc --binary schema.fbs input.json
flatc --json --strict-json schema.fbs -- input_wire.bin

Include generated-code compilation and runtime reads in the same job so compiler and runtime versions cannot silently diverge.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Schema evolution

FlatBuffers supports evolution when its rules are followed. New table fields are normally appended; existing fields should be deprecated rather than removed. Renaming breaks generated accessors and JSON names. Explicit field IDs can relax append-order requirements but do not make incompatible type or semantic changes safe. Older readers generally ignore unknown fields, while newer readers can apply defaults when older buffers lack newer fields. Run --conform in CI and review semantic changes manually. See the official evolution documentation.

Rank #4
The SQL Programming Language: .
  • Used Book in Good Condition

Troubleshooting

flatc: command not found

Install or build the compiler, put it on PATH, and confirm with flatc --version. A runtime-only package is not enough.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Unknown field or type mismatch

Check spelling, case and nesting. Ensure objects, vectors and scalars match the schema and that integers are in range. Correct the data or deliberately revise the contract; avoid untested coercion.

Invalid enum or missing root type

Use a declared enum member, or add one intentionally. A schema used as a root must declare, for example, root_type MyTable;.

Binary will not read back

Check the schema, identifier and prefix format:

flatc --json schema.fbs -- data.bin
flatc --json --raw-binary schema.fbs -- data.bin
flatc --json --size-prefixed schema.fbs -- data.bin

Use --raw-binary only when the buffer genuinely lacks an identifier; it bypasses a safety check and a mismatched schema can crash or misinterpret data. Use --size-prefixed only when the producer used that format.

JSON parser rejects familiar-looking input

Unquoted keys and trailing commas are not standard JSON. Normalize the file with a JSON parser, and use --strict-json for interoperable output.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Generated code works but behavior is wrong

Investigate root type, defaults, enum or union discriminators, schema-version mismatch, missing verification and lossy preprocessing. Round-trip fixtures usually expose the discrepancy.

When FlatBuffers is the right choice

Need Likely choice
Frequent reads, infrequent mutation, direct or low-copy access, controlled schema and multiple languages FlatBuffers
Human editing, small infrequently parsed data or maximum ecosystem interoperability JSON
Compact messages with mature RPC and service tooling Protocol Buffers
Dynamic, schema-less data while staying in the FlatBuffers family FlexBuffers via --flexbuffers
Compact dynamic maps and heterogeneous values with existing ecosystem support MessagePack, CBOR, BSON or similar

FlatBuffers does not guarantee zero application copies: strings, unpacked objects, mutations and language-runtime boundaries can allocate or copy. Benchmark the actual dataset, language, versions, compression, hardware and access methodology before claiming a win.

Deployment checklist

  • Pin flatc and runtime versions.
  • Confirm the schema has the intended root_type and identifier.
  • Match JSON names, nesting, enum values and numeric ranges.
  • Test unions, defaults, byte vectors and large integers.
  • Generate bindings from the same schema used for conversion.
  • Verify untrusted buffers before reading them.
  • Run schema-conformance and round-trip checks in CI.
  • Document whether binaries are identifier-bearing or size-prefixed.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Read next

Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.