ホーム 暮らし [For Absolute Beginners] What is a .json file? A gentle guide to its structure, syntax, and usage

[For Absolute Beginners] What is a .json file? A gentle guide to its structure, syntax, and usage

三浦 拓海 三浦 拓海 • 更新: • 公開: • 読了約 57 分
目次

When using AI tools, Python, or web services, you often come across a certain type of file.

config.json
settings.json
data.json
workflow.json
package.json

They all end with .json.

Even if an AI tells you to,

you might have had the experience of being confused, wondering:

"Is this different from a regular text file?"

"Where am I allowed to make changes?"

"What do the curly braces and colons mean?"

Many people have likely felt stuck in this situation.

JSON is a data format very commonly used in AI development, Python, web development, and application settings.

At first, it may look difficult because of all the symbols, but there aren't many basic rules.

In this article, I will explain the following about JSON for beginners:

  • What is JSON?

  • What are .json files used for?

  • Basic elements that make up JSON

  • The meaning of curly braces and square brackets

  • Differences between strings, numbers, and boolean values

  • How to open and edit JSON files

  • Common writing mistakes

  • How to read and write JSON in Python

By the time you finish reading this article, you should be able to understand what is written even when looking at JSON like the following.

 "name": "Rossy",
 "age": 3,
 "active": true,
 "skills": ["Python", "AI", "Video"]

What is JSON?

n30db587a095e

JSON is a format for organizing and storing data according to specific rules.

Its formal name is

JavaScript Object Notation

.

In Japanese, a literal translation would be

.

However, JSON today is not used only in JavaScript.

It is used in various environments such as Python, Java, C#, PHP, Go, Web APIs, and AI tools.

JSON is like a common language for different programs to exchange data.

For example, you can store information like the following.

 "name": "Rossy",
 "category": "AI",
 "published": true

This represents information such as

  • name is Rossy

  • category is AI

  • is published

in a format that is easy for programs to read.

How do you read .json?

.json is generally read as follows.

ジェイソン

For example,

config.json

In that case,

is fine to call it.

Since the pronunciation is close to the name Jason, it might feel a bit strange at first.

What is JSON used for?

JSON is mainly used for saving, configuring, and transferring data.

Saving app settings

 "theme": "dark",
 "language": "ja",
 "auto_save": true

In this example, the app's display settings are being saved.

  • Theme is dark

  • Language is Japanese

  • Auto-save is enabled

is what it means.

Saving AI tool workflows

In image generation or video generation tools, the processing flow is sometimes saved as a JSON file.

For example,

  • Model to use

  • Image size

  • Prompt

  • Node connections

  • Output destination

  • Generation count

and so on.

ComfyUI workflows are also a prime example of data saved in JSON format.

Receiving data from web services

In Web APIs, data like the following is sometimes returned in JSON.

 "weather": "sunny",
 "temperature": 28,
 "city": "Tokyo"

The program reads this data and displays the weather and temperature on the screen.

Passing information between programs

For example, you can pass information like the following from a theme management app to a video generation app.

 "theme_id": "TM-000123",
 "title": "なぜ空は青いのか",
 "status": "pending"

Using JSON makes it easier for separate programs to handle data with the same structure.

Basic structure of JSON

First, let's look at the following JSON.

 "name": "Rossy",
 "age": 3,
 "active": true

At first, it may look like a lot of symbols, but it is simple when read line by line.

name → Rossy
age → 3
active → true

In JSON, basically, you write data in the form of

項目名 : 内容

as follows.

In practice, it is expressed as follows.

"項目名": 値

For example,

"name": "Rossy"

means

.

What are keys and values?

In JSON, the item name is called a key, and its content is called a value.

"name": "Rossy"

In this case,

キー:name
値:Rossy

is.

Let's look at another example.

"age": 3

In this case,

キー:age
値:3

is.

JSON organizes data using combinations of keys and values.

You can think of it as being similar to items in an address book.

名前:Rossy
年齢:3
住所:Tokyo

When converted to JSON, it looks like this.

 "name": "Rossy",
 "age": 3,
 "address": "Tokyo"

Object

The curly braces that often appear in JSON,

 

are used to group multiple keys and values together.

This grouping is called an object.

 "name": "Rossy",
 "age": 3,
 "active": true

Inside the curly braces,

  • name

  • age

  • active

are the three pieces of information contained.

An object is

It is easy to understand if you think of it this way.

For example, if you want to summarize information for one cat, it would look like this.

 "name": "Rossy",
 "color": "white",
 "eye_color": "blue"

[ ] Array

Square brackets,

[ ]

are used when listing multiple values in order.

This collection is called an array.

["Python", "AI", "Git"]

This is an array containing three strings.

Using an array as a key value

 "skills": ["Python", "AI", "Git"]

This means,

.

Listing multiple people or data entries

You can also include multiple objects within an array.

[
 
 "name": "Rossy",
 "color": "white"
 ,
 
 "name": "Lumi",
 "color": "black"
 
]

This is data for two entries.

1件目:Rossy、白
2件目:Lumi、黒

This means the following.

Data types that can be used in JSON

In JSON, you can use several types of values.

These types are called data types.

The following six types are mainly used.

String

A string represents characters or text.

"name": "Rossy"

Strings must always be enclosed in double quotes,

" "

enclosed by.

"message": "こんにちは"

Even if it looks like a number, if it is enclosed in double quotes, it is a string.

"id": "001"

This is not a numerical value, but 001 as text.

Number

Numbers are written without being enclosed in double quotes.

"age": 3

Decimals can also be used.

"score": 98.5

Negative numbers can also be used.

"temperature": -2

The following two look similar, but have different meanings.

"count": 10

This is a number.

"count": "10"

This is a string.

They are treated as different things in a program.

Boolean

A boolean represents whether something is true or false, valid or invalid.

"active": true
"active": false

The following two values can be used.

true
false

In JSON, they are written in lowercase.

true

is used to mean valid, on, or yes.

false

is used to mean invalid, off, or no.

null

null represents that a value does not exist.

"published_at": null

This means,

is what it signifies.

It is different from an empty string.

"published_at": ""

This is an empty string.

"published_at": null

This is a state where the value itself is absent.

Object

An object is data enclosed in curly braces.

 "user": 
 "name": "Rossy",
 "age": 3
 

Inside user, there are further name and age fields.

Array

An array is data enclosed in square brackets.

 "tags": ["AI", "Python", "初心者"]

You can store multiple values together.

What is a nested structure?

In JSON, you can put objects inside objects, or objects inside arrays.

This kind of structure is called a nested structure.

 "user": 
 "name": "Rossy",
 "profile": 
 "color": "white",
 "eye_color": "blue"
 
 

If you visualize the structure, it looks like this.

user
├─ name
└─ profile
 ├─ color
 └─ eye_color

It is a state where small boxes are inside a large box.

Combining arrays and objects

 "project": "Rossy Laboratory",
 "members": [
 
 "name": "Rossy",
 "role": "guide"
 ,
 
 "name": "Lumi",
 "role": "support"
 
 ]

In this JSON,

  • The project is Rossy Laboratory

  • There are two pieces of data in members

  • The first is Rossy

  • The second is Lumi

This represents that information.

Even if a JSON file becomes long, you can understand its structure by following the matching curly and square brackets.

How to open a JSON file

A JSON file is basically a text file.

Therefore, you can open it with apps such as the following:

  • Notepad

  • Visual Studio Code

  • Cursor

  • Notepad++

  • Various text editors

Visual Studio Code is recommended

Visual Studio Code is recommended even for beginners.

When you open a JSON file in VSCode,

  • keys

  • strings

  • numbers

  • Symbols

and so on will be color-coded.

Also, a red wavy line may appear where there is a syntax error.

While you can edit them in Notepad, it is difficult to spot mistakes, so code editors like VSCode are better suited for editing JSON.

How to edit JSON files

For example, suppose you have the following configuration file.

 "language": "en",
 "theme": "light",
 "auto_save": false

If you want to change it to Japanese, dark theme, and enable auto-save, edit it as follows.

 "language": "ja",
 "theme": "dark",
 "auto_save": true

Only the values have been changed.

en → ja
light → dark
false → true

Do not change keys arbitrarily

For example,

"language": "ja"

the language in the code is the item name that the program is looking for.

If you change this on your own to,

"言語": "ja"

the program may no longer be able to find the setting.

Unless instructed by AI or a manual, basically remember to change the value, not the key.

Basic rules for writing JSON

There are several important syntax rules for JSON.

Keys must be enclosed in double quotes

This is a correct example.

 "name": "Rossy"

This is an incorrect example.

 name: "Rossy"

In JSON, keys must be enclosed in double quotes.

Enclose strings in double quotation marks

This is a correct example.

 "name": "Rossy"

This is an incorrect example.

 "name": Rossy

Double quotation marks are required to indicate that Rossy is a string.

Do not use single quotation marks

In JSON, you generally cannot use single quotation marks,

' '

or apostrophes.

This is an incorrect example.

 'name': 'Rossy'

Correctly, you should use double quotation marks.

 "name": "Rossy"

Separate items with commas

 "name": "Rossy",
 "age": 3,
 "active": true

There is a comma at the end of each item.

However, do not add a comma to the last item.

Do not add a comma to the last item

This is a correct example.

 "name": "Rossy",
 "age": 3

This is an incorrect example.

 "name": "Rossy",
 "age": 3,

There is an extra comma after the final 3.

This is sometimes called a trailing comma.

While some programs may allow it, it cannot be used in standard JSON.

Correctly close curly and square brackets

Always close an opened symbol with its corresponding closing symbol.

 → 
[ → ]

This is a correct example.

 "tags": ["AI", "Python"]

This is an incorrect example.

 "tags": ["AI", "Python"

The square bracket is not closed.

Common JSON errors

After editing a JSON file, sometimes an app may fail to start or display a loading error.

Most of these are caused by small symbol mistakes.

Missing comma

This is an incorrect example.

 "name": "Rossy"
 "age": 3

There is no comma after "Rossy".

The correct way to write it is as follows.

 "name": "Rossy",
 "age": 3

Too many commas

 "name": "Rossy",
 "age": 3,

There is a comma after the last item.

The correct way is as follows.

 "name": "Rossy",
 "age": 3

Forgetting to close the double quotation marks

This is an incorrect example.

 "name": "Rossy

There is no double quotation mark after Rossy.

The correct way is as follows.

 "name": "Rossy"

Using uppercase for true or false

This is an incorrect example.

 "active": True

In JSON, you must write them in lowercase.

 "active": true

Since Python uses True and False, this is a part that is easy to confuse.

Using full-width characters

When typing in Japanese, full-width symbols can sometimes get mixed in.

For example, symbols like the following.

{ }
[ ]
:
,
“
”

Even if they look similar, they are not correctly recognized in JSON.

Symbols must be entered in half-width.

 
[ ]
:
,
"

The number of curly braces does not match

In long JSON files, it can become difficult to tell which curly brace corresponds to which.

Therefore, it is important to write with indentation.

This is an example that is hard to read.

"user":"name":"Rossy","skills":["Python","AI"]

The content is the same, but the following is easier to read.

 "user": 
 "name": "Rossy",
 "skills": [
 "Python",
 "AI"
 ]
 

You cannot write comments in JSON

You might want to leave explanations in a program's configuration file.

For example, you might want to write it like this.

 // 表示言語
 "language": "ja"

However, in official JSON, you cannot write comments using //.

Comments like the following cannot be used either.

 /* 自動保存を有効にする */
 "auto_save": true

If you want to leave an explanation

If the program allows it, there is a way to prepare a key for explanations.

 "_comment": "表示言語の設定",
 "language": "ja"

However, depending on the program, having unexpected keys might cause issues.

Do not add them on your own; please check the specifications.

Note that some formats similar to JSON allow the use of comments.

For example,

  • JSONC

  • JSON5

  • proprietary configuration formats used by some applications

and so on.

Even if the extension is .json, some applications may independently allow comments, but it is safer to remember that comments are not allowed in standard JSON.

What is the difference between JSON and Python dictionaries?

When you start learning Python, you will encounter syntax that looks very similar to JSON.

data = 
 "name": "Rossy",
 "age": 3,
 "active": True

This is a Python dictionary.

It is very similar to JSON.

In JSON, you write it as follows.

 "name": "Rossy",
 "age": 3,
 "active": true

The main differences are as follows.

Content JSON Python True true True False false False No value null None String Double quotes Both single and double allowed Comments Generally not allowed Can be written with #

For example, in JSON,

 "active": true,
 "message": null

In Python,

 "active": True,
 "message": None

it becomes.

Because they look similar, be careful when copying.

Reading JSON in Python

Python comes with a built-in feature called json for handling JSON.

No additional installation is required.

For example, let's assume there is a file named config.json.

 "name": "Rossy",
 "age": 3,
 "active": true

The code to read it in Python is as follows.

import json

with open("config.json", "r", encoding="utf-8") as file:
 data = json.load(file)

print(data)

When executed, it is read into Python as a dictionary.

'name': 'Rossy', 'age': 3, 'active': True

Let's look at the meaning line by line.

import json

This imports the functionality for handling JSON in Python.

with open("config.json", "r", encoding="utf-8") as file:

This opens config.json in read mode.

config.json 開くファイル
r 読み取りモード
encoding="utf-8" 日本語を正しく扱うための文字コード
data = json.load(file)

This converts the contents of the JSON file into data that can be used in Python.

print(data)

This displays the loaded content on the screen.

Writing JSON in Python

You can also save Python data as a JSON file.

import json

data = 
 "name": "Rossy",
 "age": 3,
 "active": True

with open("output.json", "w", encoding="utf-8") as file:
 json.dump(data, file, ensure_ascii=False, indent=2)

When executed, a file named output.json is created.

 "name": "Rossy",
 "age": 3,
 "active": true

What is json.dump?

json.dump(data, file, ensure_ascii=False, indent=2)

The meanings of each are as follows.

data 保存するデータ
file 保存先のファイル
ensure_ascii=False 日本語をそのまま保存する
indent=2 読みやすいように字下げする

If you do not specify ensure_ascii=False, Japanese characters may be saved in the following format.

\u30ed\u30c3\u30b7\u30fc

This does not mean it is broken, but it becomes difficult for humans to read.

For JSON containing Japanese,

ensure_ascii=False

specifying this makes it easier to read.

Example of a configuration file using JSON

Let's look at an example closer to an actual configuration file.

 "app": 
 "name": "Sample Generator",
 "version": "1.0.0"
 ,
 "output": 
 "directory": "outputs",
 "format": "mp4",
 "overwrite": false
 ,
 "generation": 
 "model": "sample-model",
 "width": 1080,
 "height": 1920,
 "steps": 30
 

This JSON contains three major groups.

app
output
generation

app

"app": 
 "name": "Sample Generator",
 "version": "1.0.0"

Contains the app name and version.

output

"output": 
 "directory": "outputs",
 "format": "mp4",
 "overwrite": false

Settings related to output.

  • Save location is outputs

  • Format is mp4

  • Do not overwrite existing files

This is what it means.

generation

"generation": 
 "model": "sample-model",
 "width": 1080,
 "height": 1920,
 "steps": 30

Settings related to generation.

  • Model used

  • Width

  • Height

  • Number of processing iterations

are saved here.

Grouping related settings like this makes it easier to organize data.

Points to check when an AI provides you with a JSON

When you consult an AI about modifying a program, it may instruct you to change the JSON.

Before you rewrite it, let's check the following points.

Which file to modify

There may be multiple JSON files with the same name.

For example,

config.json
settings/config.json
backup/config.json

Even if you edit a different file, the settings will not be reflected.

It is safe to confirm the full path of the file with the AI.

変更対象のJSONファイルを、フルパスで教えてください。

Which key to modify

Ask for clarification on the before and after changes.

変更前と変更後のJSONを、該当部分だけ提示してください。

Are you adding a key, or just changing a value?

The following two are different operations.

Changing a value.

"enabled": false

From,

"enabled": true

Change to.

Adding a key.

 "enabled": true,
 "timeout": 600

A new item called timeout has been added.

Back up the file

It is safe to copy the file before making changes.

config.json
config_backup.json

If a problem occurs, you can revert to the original file.

Do not replace the entire JSON

If you delete the entire file and replace it when the AI only provided a part of the corrected version, other settings may be lost.

Confirm whether you should 'change only the relevant part' or 'replace the entire file'.

How to verify if JSON is correct

After editing a JSON file, check if the syntax is correct.

In VSCode, missing symbols or similar issues may be indicated by a red wavy line.

Alternatively, you can use Python to verify it.

python -m json.tool config.json

If the JSON is correct, the formatted content will be displayed.

If there is an issue with the syntax, the location of the error will be displayed.

For example,

Expecting ',' delimiter

if this is displayed, there may be a missing comma.

Expecting property name enclosed in double quotes

if this is displayed, the key may not be enclosed in double quotes.

This command does not change the content of the JSON and can be used to verify the syntax.

Just remember these basics and you will be fine

Here is a summary of the points you should remember first about JSON.

JSON is a format for organizing and storing data

 "name": "Rossy"

It is basically a combination of keys and values

"キー": 値

Curly braces represent objects

 "name": "Rossy",
 "age": 3

Square brackets represent arrays

["AI", "Python", "Git"]

Strings must be enclosed in double quotes

"name": "Rossy"

Numbers should not be enclosed in double quotes

"age": 3

Boolean values are lowercase

"active": true

Items must be separated by commas

 "name": "Rossy",
 "age": 3

Do not add a comma to the last item

 "name": "Rossy",
 "age": 3

You cannot write comments in standard JSON

// このようなコメントは原則使えない

If you understand these 10 points, you will be able to read basic JSON files well enough.

Conclusion

When you first see JSON, it looks difficult with all the curly braces, square brackets, colons, and commas lined up.

However, the basics are very simple.

 "name": "Rossy",
 "age": 3,
 "skills": ["AI", "Python"]

If you translate this JSON into human language, it is just

名前はRossy
年齢は3
スキルはAIとPython

information.

JSON is like organizing information using boxes and labels.

  • The key is the label

  • The value is the content

  • The curly braces are a single box

  • The square brackets are a shelf for lining up multiple items

Thinking of it this way makes it easier to read.

As you continue with AI development or Python, you will encounter JSON files many times.

You don't need to memorize everything from the start.

First, try starting by reading

from the file.

Once you can read JSON, you will gradually be able to understand AI tool settings, error messages, and data integration between programs.

Thank you for reading until the end!

If this article was even a little bit helpful, I would be happy if you could click the "Like" button!

I will continue to publish practical and easy-to-understand articles that I hope will make you think, "I wish I had an article like this when I was a beginner."

See you in the next article. Have a great development life!

同じカテゴリの記事

三浦 拓海
コンテンツディレクター 三浦 拓海
プロフィールを見る →

映画と連続ドラマの公開情報を追う担当。配給会社のプレスシートと公式サイトの発表日を確認したうえで執筆します。

おすすめの関連記事