JSON Primer

JSON入門

CouchDB uses JavaScript Object Notation (JSON) for data storage, a lightweight format based on a subset of JavaScipt syntax. One of the best bits about JSON is that it’s easy to read and write by hand, much more so than something like XML. We can parse it naturally with JavaScript because it shares part of the same syntax. This really comes in handy when we’re building dynamic web applications and we want to fetch some data from the server.

CouchDBはJavaScript Object Notation(JSON)という、JavaScriptの構文のサブセットを基にした軽量フォーマットをデータストレージに使用しています。JSONの最も良い点として、XMLのようなものと比べて、手作業での読み書きがずっと簡単であることであることが挙げられます。JSONはJavaScriptと同じ構文を利用しているので、当然JavaScriptで解析することができます。この点は、動的なウェブアプリケーションを構築するときや、サーバーからデータを取得するときに本当に役立ちます。

Here’s a sample JSON document:

以下にJSONによるドキュメントの例を示します。

{
    "Subject": "I like Plankton",
    "Author": "Rusty",
    "PostedDate": "2006-08-15T17:30:12-04:00",
    "Tags": [
        "plankton",
        "baseball",
        "decisions"
    ],
    "Body": "I decided today that I don't like baseball. I like plankton."
}

You can see that the general structure is based around key/value pairs and lists of things.

JSONの一般的な構造は、キーと値のペアとリストを基礎にしていることが分かります。

Data Types

データ型

JSON has a number of basic data types you can use. We’ll cover them all here.

JSONでは、いくつかの基本的なデータ型を使用することができます。ここでは、それらのすべてをカバーしたいと思います。

Numbers

数値

You can have positive integers: "Count": 253

正の整数を使うことができます。"Count": 253

Or negative integers: "Score": -19

負の整数も使うことができます。"Score": -19

Or floating-point numbers: "Area": 456.31

浮動小数点数を使うこともできます。"Area": 456.31

There is a subtle but important difference between floating-point numbers and decimals. When you use a number like 15.7, this will be interpreted as 15.699999999999999 by most clients, which may be problematic for your application. For this reason, currency values are usually better represented as strings in JSON. A string like "15.7" will be interpreted as "15.7" by every JSON client.

浮動小数点数と小数との間には、微妙ですが重要な違いがあります。15.7という数値を使う場合、これをほとんどのクライアントは15.699999999999999と解釈します。このことはアプリケーションにとって問題となるかも知れません。このような理由から、JSONでは通常、通貨の値については文字列として表現する方が良いといえます。"15.7"という文字列は、どのJSONクライアントも"15.7"と解釈します。

Or scientific notation: "Density": 5.6e+24

指数表記を使うこともできます。"Density": 5.6e+24

Strings

文字列

You can use strings for values:

文字列を値として使うことができます。

"Author": "Rusty"

You have to escape some special characters, like tabs or newlines:

タブや改行など、いくつかの特殊な文字はエスケープしなければなりません。

"poem": "May I compare thee to some\n\tsalty plankton."

The JSON site has details on what needs to be escaped.

JSONのサイトには、どの文字列をエスケープしなければならないかについての詳細が掲載されています。

Booleans

真偽値

You can have boolean true values:

ブール値のtrueを使うことができます。

"Draft": true

Or boolean false values:

ブール値のfalseを使うこともできます。

"Draft": false

Arrays

配列

An array is a list of values:

配列は値のリストです。

"Tags": ["plankton", "baseball", "decisions"]

An array can contain any other data type, including arrays:

配列にはどんなデータ型でも入れることができます。配列に配列を入れることもできます。

"Context": ["dog", [1, true], {"Location": "puddle"}]

Objects

オブジェクト

An object is a list of key/value pairs:

オブジェクトはキーと値のペアのリストです。

{"Subject": "I like Plankton", "Author": "Rusty"}

Nulls

You can have null values:

null値を使うことができます。

"Surname": null