You are on page 1of 13

Json – JavaScript

Object Notation
What is Json?
When exchanging data between a browser and a server, the data can only be text.
JSON is text, and we can convert any JavaScript object into JSON, and send JSON to the
server.
We can also convert any JSON received from the server into JavaScript objects.

• JSON stands for JavaScript Object Notation


• JSON is "self-describing" and easy to understand
• JSON is language independent - JSON uses JavaScript syntax, but the JSON format is
text only. Text can be read and used as a data format by any programming language.
JSON Syntax Rules
JSON syntax is derived from JavaScript object notation syntax:
• Data is in name/value pairs
• Data is separated by commas
• Curly braces hold objects
• Square brackets hold arrays

JSON data is written as name/value pairs.


A name/value pair consists of a field name (in double quotes), followed by a colon,
followed by a value:
“name”: “Mark”
JSON names require double quotes. JavaScript names don't.
JSON - Evaluates to JavaScript Objects
The JSON format is almost identical to JavaScript objects.
In JSON, keys must be strings, written with double quotes:
{ "name":"John" }
In JSON, values must be one of the following data types:
• a string
• a number
• an object (JSON object)
• an array
• a boolean
JSON - Evaluates to JavaScript Objects
In JavaScript values can be all of the above, plus any other valid JavaScript expression,
including:
• a function
• Undefined
In JSON, string values must be written with double quotes:
{ "name": "John" }
In JavaScript, you can write string values with double or single quotes:
{ name: 'John' }
JSON Data Types
Strings in JSON must be written in double quotes.
{ "name":"John" }
Numbers in JSON
{ "age":30 }
Values in JSON can be objects.
{
"employee":{ "name":"John", "age":30, "city":"New York" }
}
Values in JSON can be arrays.
{
"employees":[ "John", "Anna", "Peter" ]
}
JSON Data Types
Values in JSON can be true/false.
{ "sale":true }
JSON Objects
{ "name":"John", "age":30}

• JSON objects are surrounded by curly braces {}.


• JSON objects are written in key/value pairs.
• Keys must be strings, and values must be a valid JSON data type (string, number,
object, array, boolean).
• Keys and values are separated by a colon.
• Each key/value pair is separated by a comma.
Accessing Object Values
You can access the object values by using dot (.) notation:
myObj = { "name":"John", "age":30 };
x = myObj.name;
You can also access the object values by using bracket ([]) notation:
myObj = { "name":"John", "age":30 };
x = myObj["name"];
Nested JSON Objects
Values in a JSON object can be another JSON object.
myObj = { You can access nested JSON objects:
"name":"John", x = myObj.cars.car2;
"age":30, x = myObj.cars["car2"];
"cars": {
"car1":"Ford",
"car2":"BMW",
"car3":"Fiat"
}
}
Arrays in Json
Arrays can be values of an object property:
myObj = {
"name":"John",
"age":30,
"cars":[ "Ford", "BMW", "Fiat" ]
}
You access the array values by using the index number
x = myObj.cars[0];
Arrays in Json
1. Write automation tests for log in, add json file with all usernames and passwords.
https://www.saucedemo.com/index.html
2. Write automation test for adding items in the shopping cart. Verify that selected
items are in the shopping cart.
3. Write test for removing item from the shopping cart.
4. Write test for Checkout: Your information page – cover positive and negative
tests, also verify warnings.
5. Write automation test for testing purchase flow, and for adding “Checkout:Your
information” use json file with data.

You might also like