JSON Explained for Beginners: Syntax, Errors and Formats

Ta treść jest po angielsku.

JSON is the everyday language for sending structured data between websites, apps and services, and it is surprisingly easy to learn. This guide walks through the syntax rules, the errors beginners meet most often, how to format data cleanly, and how JSON compares with CSV and YAML.

What JSON is and where you will meet it

JSON stands for JavaScript Object Notation. Despite the name, it is a language-independent text format, and nearly every programming language can read and write it. You will see it in API responses, configuration files, exported data and the settings for many developer tools. Its appeal is that it is easy for both people and machines to read.

A JSON document is built from just two structures: objects, which hold named values, and arrays, which hold ordered lists. Values can be strings, numbers, true or false, null, another object or another array. That small set of rules is what makes JSON so predictable.

The core syntax rules

Objects are wrapped in curly braces, and each entry is a key followed by a colon and a value. Keys must be strings in double quotes. Arrays use square brackets, and items are separated by commas. The example below describes a small book record with a title, a list of authors, a publication year and a flag.

{ "title": "Dune", "authors": ["Frank Herbert"], "year": 1965, "inPrint": true }

Notice that the year is a bare number, not a quoted string, and that true has no quotes. Strings must always use double quotes, and whitespace between tokens does not matter, so you can lay the data out however reads best.

Five rules beginners forget

Most beginner mistakes break one of a handful of rules. Learn these five and you will avoid the majority of problems that validators report.

  • Use double quotes for every key and every string, never single quotes
  • Do not put a comma after the last item in an object or array
  • Do not include comments, because the JSON standard does not allow them
  • Escape special characters inside strings, such as writing a double quote as a backslash followed by a quote
  • Write numbers without leading zeros, so 007 is invalid and 7 is valid

The errors you will meet most often

Parsers report errors with a position, but the message can be cryptic. 'Unexpected token' usually means a stray character, often a trailing comma or a single quote. 'Unexpected end of JSON input' means the text was cut short, perhaps by a copy and paste that missed the closing brace. Keep an eye out for these patterns as you debug.

  • Trailing comma: the most common error, caused by a leftover comma at the end of a list
  • Missing comma between two entries, which the parser reads as a broken value
  • Unquoted keys, which JavaScript object literals allow but JSON does not
  • Smart quotes copied from a word processor, which look correct but are invalid
  • Comments added with two slashes or a hash, which strict parsers reject

Formatting and validating JSON

Minified JSON removes all unnecessary whitespace to save space, which suits transmission but is very hard to read. Pretty printing adds indentation so the structure is visible. Most editors and command-line tools can switch between the two. When you need to check a document, a validator will tell you where the first problem sits.

Use two or four spaces for indentation, and apply the same choice across a project. Sorting keys alphabetically makes version-to-version differences easier to spot, although the order of keys has no meaning in JSON. A JSON formatter is useful for both jobs: it indents the text and flags any error with a line number.

Working with JSON in code

Most languages have a built-in parser. In JavaScript, JSON.parse turns text into an object and JSON.stringify turns an object back into text. In Python, the json module offers loads and dumps for strings. Always handle parse errors, because input from users or external services cannot be assumed to be well formed.

Mind the types. JSON has no date type, so dates are usually stored as ISO 8601 strings such as 2026-03-14T09:30:00Z. JSON also has no separate integer type, and very large numbers can lose precision in JavaScript, so store large identifiers as strings.

JSON compared with CSV and YAML

CSV is a flat table of rows and columns, ideal for spreadsheets and simple exports, but it has no way to represent nested data and no standard for types. JSON handles nested structures naturally but is more verbose for plain tables. YAML is designed for human editing, supports comments and uses indentation rather than braces, which is why it is popular for configuration files.

YAML's indentation rules are strict and easy to break with tabs, and older YAML parsers read unquoted words such as no as false. Converting between formats is common, so the json-to-csv, csv-to-json and yaml-json-converter tools can help when you move data across.

  • Choose CSV for flat, tabular data that will open in a spreadsheet
  • Choose JSON for APIs, nested records and data exchanged between programs
  • Choose YAML for configuration files that people edit by hand

Practice steps for beginners

Reading and writing JSON becomes natural after a few sessions. Start with a small object describing yourself, paste it into the json-formatter, and fix any errors it reports. Then add a nested object, watch how the indentation changes, and convert the same data to CSV to see what is lost. Keep the rules in mind, check every change with a validator, and you will rarely be stuck for long.

  • Write a small object with a name, a list of hobbies and a true or false flag
  • Paste it into the json-formatter and fix any errors it reports
  • Add a nested object and see how the indentation changes
  • Convert the same data to CSV and compare the two versions

Narzędzia wspomniane w poradniku

Najczęstsze pytania

Can JSON have comments?

Officially, no. The JSON specification does not provide a comment syntax, so a strict parser will reject lines starting with two slashes or a hash. Some tools accept comments in files they call JSON with comments, and certain editors allow them in their own settings files. If you need comments in configuration, YAML or a format designed for that purpose may suit you better than standard JSON.

What is the difference between JSON and a JavaScript object?

JSON is a text format, while a JavaScript object is a value that exists in memory. JSON keys must be double-quoted strings, but JavaScript object literals allow unquoted keys, single quotes, functions and undefined values. The text you send over a network is JSON, and JSON.parse converts it into an object. Mixing the two up is a common source of trailing comma and quote errors.

Why does my JSON fail validation because of a trailing comma?

JSON does not allow a comma after the final item of an object or array, even though JavaScript code often does. Parsers are strict, so a single trailing comma stops the whole document from loading. Delete the comma that comes before the closing brace or square bracket. Editors that highlight syntax often show the problem immediately, and a validator will point to the exact line.

Should I use JSON or CSV for exporting data?

Use CSV when the data is a flat table and the recipient will open it in a spreadsheet, since it is compact and widely supported. Use JSON when records contain nested lists or objects, when the data will feed another program, or when types such as booleans and numbers matter. CSV files can also differ in quoting and encoding between applications, so test the file in the tool your recipient will use.

Is YAML better than JSON for configuration?

YAML is often easier to read for configuration because it uses indentation, supports comments and avoids much of the punctuation that JSON requires. JSON is stricter and more predictable, and almost every language parses it quickly. Many projects use YAML for files that humans edit and JSON for machine-generated data. The best choice depends on who edits the file and which tools read it.