Skip to main content

Key-Value Pair

A key-value pair is a set of two linked data items, where the key is a unique identifier that is used to retrieve or reference the corresponding value. In JSON, a key-value pair is represented as an object, which is a set of key-value pairs enclosed in curly braces . Each key is followed by a colon : and the corresponding value.

Key-Value Pair Syntax

The syntax for a key-value pair as follows:

{
"key": value
}

In this syntax, key is a string that serves as the identifier for the value. The value can be any valid data type, such as a string, number, boolean, array, or another object.

Example
{
"name": "John",
"age": 30,
"isStudent": true
}

In this example, "name", "age", and "isStudent" are the keys, and "John", 30, and true are the corresponding values.

Nested Key-Value Pairs

Nested key-value pairs within other key-value pairs create complex data structures. Here's an example of a nested key-value pair:

Nested example
{
"person": {
"name": "Jane",
"age": 25
}
}

In this example, "person" is the key for an object that contains two key-value pairs: "name" and "age". The value of "person" is therefore another key-value pair that contains nested data.