Skip to main content

JSONとPHP

JSONのファイル形式について

JSON is a way to store and organize data inside a text file. It is frequently used in web applications to send data between the client and the server.

JSON files store a combination of dictionaries (associative arrays) and indexed arrays. Please note that in JSON, dictionaries are called "objects"

Objects

Objects are a collection of pairs of keys and values, separated by commas, stored between curly braces like so

{
    "key 1" : "value 1",
    "key 2" : "value 2",
    ...
    "key n" : "value n"
}

where "key" is always a string (defined between quotation marks) and value can be one of the following types:

String (between quotation marks)

"player_name" : "ウルトラマン"
Integer numbers
 "XP" : 12345
Floating point numbers
"distance" : 31.145
Boolean (true / false)
"success" : true
Other object or array
"item_data" : 
  { 
    "name:" : "魔法のマント",
    "cost"  : 300
  }

Arrays

Arrays are sequential values, usually of the same type. Arrays are defined by square brackets:

[
  "Apple", 
  "Banana", 
  "Grape"
]

JSON does allow mixed types within the same array. While strange, this is also possible:

[
  "Apple", 
  25, 
  false
]

Example

A JSON file must always start with an curly brace (as an object), or with a square bracket (as an array). In our practice, we'll prefer starting as an object, since it is easier to understand.

Let's imagine A JSON that handles data about a save game. We want to store the date, the XP, HP, and list of stages cleared. It would look something like this:

{
	"save_date" : "2026-07-14",
	"save_time" : "11:45",
	"XP" : 54796
	"HP" : 97.15
	"stage_cleared" : [true, true, false, false, true, false]
}

JSONとPHPのデータ変換

Since JSON is used a lot in web contexts, PHP provides two functions to convert a PHP associative array into JSON text and vice-versa.

From PHP associative array to JSON text:
$json = json_encode($dictionary);
From JSON text to PHP associative array
$dictionary = json_decode($json, true);

練習

Let's take what we saw and run a little exercise.

  • Download this JSON file and place it in your local XAMPP webserver (C:\xampp\htdocs).
  • Load the JSON file using the PHP instruction file_get_contents (textbook page 80)
  • Change the save_date and save_time to current time and date (textbook page 40)
  • Mark the third stage (index 2), as cleared (set to true)
  • Convert back to JSON text 
  • Display the JSON text so the browser can display it
test_json.php
<?php	
	// JSONを読み込む(テキスト)
	$json = file_get_contents("save_slot.json");

	// PHPの連想配列へ変換
	$data = json_decode($json, true);
	
	// 現在の日時にする
	$data["save_date"] = date("Y-m-d");
	$data["save_time"] = date("H:i:s");
	
	// 3つ目のステージをクリアにする
	$data["stage_cleared"][2] = true;
	
	// またJSONへ
	$json = json_encode($data);
	
	// 表示(返す)
	print($json);
?>

 

Explicitly returning JSON

By default, Apache (the webserver) assumes that the result is an HTML file, so it tells the browser to expect an HTML file. It does so, by defining a "header", in which the type of the data is defined. These are known as MIME types and you can find a list here

In this case, we want to tell the browser that the result is not an HTML file but a JSON file. We can do so by using the PHP header() instruction:

// JSONを返すべき
header("Content-Type: application/json; charset=utf-8");

Change the header on the exercise before and see how it changes the result.