JSON
JSON – Java Script Object Notation
• JSON is a syntax for storing and exchanging data.
• Basically, It was designed for human-readable data interchange.
• JSON is text, written with Java Script Object Notation.
• It has been extended from the JavaScript scripting language
• The filename extension is .json
• JSON internet Media type is application/json
JSON Data Types
1. String
A string in JSON is a sequence of characters enclosed in double quotes ("). It can include letters,
numbers, spaces, and special characters.
Example:
{
"name": "John Doe",
"city": "New York",
"status": "Active"
}
2. Number
Numbers in JSON can be integers or floating-point (decimal) values. They do not require quotes.
Example:
{
"age": 30,
"height": 5.9,
"score": 100
}
3. Boolean
Boolean values can either be true or false. They represent binary states and are not enclosed in
quotes.
Example:
{
"isStudent": false,
"isEmployed": true
}
4. Null
null is a special value in JSON that represents the absence of a value. It is not enclosed in quotes.
Example:
{
"middleName": null,
"spouse": null
}
5. Array
An array is an ordered list of values. The values can be of any JSON data type (e.g., strings, numbers,
objects, etc.). Arrays are enclosed in square brackets ([]).
Example:
{
"skills": ["Java", "Python", "Selenium"],
"scores": [85, 90, 95]
}
6. Object
An object is a collection of key-value pairs enclosed in curly braces ({}). Keys are strings, and values
can be any JSON data type.
Example:
{
"person": {
"firstName": "John",
"lastName": "Doe",
"address": {
"city": "New York",
"zip": "10001"
}
}
}
7. Example Combining All Data Types
Here is a more complex example that combines all JSON data types:
Example:
{
"name": "Alice",
"age": 28,
"isMarried": false,
"children": null,
"hobbies": ["reading", "traveling", "coding"],
"address": {
"city": "Los Angeles",
"state": "California",
"postalCode": "90001"
},
"scores": [95.5, 88.0, 76],
"profileComplete": true
}
JSON Path
• JSON Path is a query language for JSON data, similar to XPath for XML.
• It allows you to navigate and extract specific data from a JSON document by specifying paths.
• JSON Path uses a syntax that makes it easy to access elements, objects, arrays, and their
nested components.
JSON Path Syntax Overview
1. Dot Notation (.): Accesses child elements.
2. Bracket Notation ([]): Accesses array elements or specific keys.
3. Wildcard (*): Matches all elements.
Sample JSON Data
Here’s an example JSON object that we’ll use to demonstrate JSON Path queries:
{
"store": {
"book": [
{
"title": "Java Programming",
"author": "John Doe",
"price": 29.99,
"categories": ["Programming", "Java"]
},
{
"title": "Python Basics",
"author": "Jane Smith",
"price": 19.99,
"categories": ["Programming", "Python"]
}
],
"bicycle": {
"type": "Mountain",
"price": 499.99
}
}
}
JSON Path Examples
1. Accessing a Simple Key
o Path: $.[Link]
o Result: "Mountain"
2. Accessing a Nested Key
o Path: $.[Link][0].author
o Result: "John Doe"
3. Accessing All Elements in an Array
o Path: $.[Link][*].title
o Result: ["Java Programming", "Python Basics"]
4. Accessing a Specific Element in an Array
o Path: $.[Link][1].title
o Result: "Python Basics"
5. Using a Wildcard to Access All Data in an Array
o Path: $.[Link][*]
o Result:
[
{
"title": "Java Programming",
"author": "John Doe",
"price": 29.99,
"categories": ["Programming", "Java"]
},
{
"title": "Python Basics",
"author": "Jane Smith",
"price": 19.99,
"categories": ["Programming", "Python"]
}
]
Useful tools to generate and evaluate JSON Paths:
[Link]
[Link]
[Link]