Skip to content

JSON Serialize Online

See how your data serializes to JSON with different settings: compact or indented, camelCase or snake_case names, nulls kept or left out. Then copy the matching settings for C#, Java, Python or JavaScript.

object → JSON text

Valid JSON · object with 5 keys · 3 levels deep · 215 bytes

Names
Format
{
  "orderId": 1048,
  "customerName": "Zoë Brown",
  "giftMessage": null,
  "items": [
    {
      "sku": "MUG-01",
      "unitPrice": 8.5,
      "qty": 2
    },
    {
      "sku": "TEE-M",
      "unitPrice": 15,
      "qty": 1
    }
  ],
  "isPaid": true
}

Everything runs in your browser; nothing is uploaded.

What does it mean to serialize JSON?

Serializing turns objects in your program into JSON text, so they can be sent in an API response, saved to a file or put on a queue. Deserializing is the reverse, reading the text back into objects.

The same object can serialize to different text depending on the serializer’s settings: whether it is indented, how property names are written, whether null values appear, and whether characters like é are escaped. The data is identical, but the other side of an API often expects one exact style, such as camelCase names, which is why these settings matter.

Serializing is sometimes called encoding (PHP’s json_encode) or stringifying (JavaScript’s JSON.stringify).

How to use the JSON serializer

  1. Paste a sample of your data as JSON, for example the output your code produces today.
  2. Choose the settings the receiving side expects: a naming style, compact or indented output, and whether to leave out nulls.
  3. Check the JSON tab for the exact result, then open your language’s tab to copy the serializer settings that produce it.

If two keys collapse into one name, for example user_id and userId both becoming userId, the tool warns you.

Example: C# property names to a camelCase API

A C# order has PascalCase properties, which is what you get when you serialize it with no settings. The JavaScript front end expects camelCase names and no nulls.

{ "OrderId": 1048, "CustomerName": "Zoë Brown", "GiftMessage": null, "Items": [ { "Sku": "MUG-01", "UnitPrice": 8.5, "Qty": 2 }, { "Sku": "TEE-M", "UnitPrice": 15, "Qty": 1 } ], "IsPaid": true }

Step 1: Set the options

using System.Text.Json; using System.Text.Json.Serialization; using System.Text.Encodings.Web; var options = new JsonSerializerOptions { WriteIndented = true, PropertyNamingPolicy = JsonNamingPolicy.CamelCase, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping, // keeps é, ü… as they are }; string json = JsonSerializer.Serialize(data, options);

Step 2: The serialized JSON

{ "orderId": 1048, "customerName": "Zoë Brown", "items": [ { "sku": "MUG-01", "unitPrice": 8.5, "qty": 2 }, { "sku": "TEE-M", "unitPrice": 15, "qty": 1 } ], "isPaid": true }

Every name is now camelCase and GiftMessage is gone. Zoë keeps her ë because of the relaxed encoder; without it, System.Text.Json writes Zoë.

How each language serializes by default

With no settings at all, the common serializers give slightly different JSON for the same data:

Default JSON serialization behaviour in common languages
SerializerSpacingNamesNullsé
C# System.Text.Jsoncompactas declaredwritten\u00E9
C# Newtonsoft.Jsoncompactas declaredwrittené
Java Jacksoncompactas declaredwrittené
Python json.dumps", " and ": "dict keysnull\u00e9
JavaScript JSON.stringifycompactas in the objectwrittené

Two of these catch people out. Python’s default output isn’t fully compact, because it puts a space after every comma and colon: pass separators=(",", ":") for the smallest output. System.Text.Json escapes every non-ASCII character, and also < > & ' +, which is safe for HTML but hard to read. Use JavaScriptEncoder.UnsafeRelaxedJsonEscaping for JSON that isn’t embedded in HTML.

Common serialization errors

Common JSON serialization errors, their causes and fixes
ErrorCauseFix
A possible object cycle was detected (C#) · Self referencing loop detected (Newtonsoft)Objects point at each other, e.g. order → customer → ordersReferenceHandler.IgnoreCycles, or serialize a simpler DTO
Converting circular structure to JSON (JavaScript) · Circular reference detected (Python)The same loop in JavaScript or PythonRemove the back-reference before serializing
Object of type datetime is not JSON serializablePython’s json only knows dicts, lists, strings, numbers, bools and Nonejson.dumps(data, default=str), or .isoformat()
Do not know how to serialize a BigIntJSON.stringify can’t write BigInt valuesConvert to a string first
Enums come out as numbersC# writes enum values, not names, by defaultAdd JsonStringEnumConverter

Frequently asked questions

What is the difference between serialize and stringify?

They mean the same thing: turning data into JSON text. JavaScript calls it stringify (JSON.stringify), PHP calls it encode (json_encode), and C#, Java and Python libraries call it serialize. "Stringify JSON" can also mean escaping JSON into a string, which is a second, separate step.

How do I serialize to camelCase in C#?

With System.Text.Json, set PropertyNamingPolicy = JsonNamingPolicy.CamelCase in JsonSerializerOptions (ASP.NET Core already does this for web APIs). With Newtonsoft, use a DefaultContractResolver with CamelCaseNamingStrategy.

Should I leave null values out of JSON?

It makes responses smaller, but only if the receiving code treats a missing key the same as null. In JavaScript a missing key reads as undefined, not null, so check the other side before you turn it on.

Does serializing change my numbers or key order?

Key order follows your class or object, unless you sort it. Numbers keep their value, but formatting can change: JavaScript and PHP write 10.0 as 10, while Newtonsoft.Json writes a whole double such as 15 as 15.0. JSON readers treat both forms the same.

Sources