🔥 0
0

Working with JSON

JSON (JavaScript Object Notation) is the standard data format for APIs. Every e-commerce API response you'll test will be in JSON.

JSON Basics

Syntax Rules

{
  "string": "Hello World",
  "number": 99.99,
  "boolean": true,
  "null": null,
  "array": [1, 2, 3],
  "object": { "nested": "value" }
}
Rules:
  • Keys must be in double quotes "key"
  • Strings use double quotes "value"
  • No trailing commas
  • No comments allowed

JSON Data Types

TypeExampleE-commerce Use
String"iPhone 15"Product names
Number999.99Prices, quantities
Booleantrue / falseIn stock, featured
NullnullNo coupon applied
Array[...]List of products
Object{...}Single product

---

E-commerce JSON Examples

Product Object

{
  "id": "PROD-001",
  "name": "Wireless Headphones",
  "price": 79.99,
  "originalPrice": 99.99,
  "inStock": true,
  "stockCount": 45,
  "category": "electronics",
  "tags": ["wireless", "bluetooth", "audio"],
  "rating": {
    "average": 4.5,
    "count": 128
  },
  "images": [
    "https://shop.com/img1.jpg",
    "https://shop.com/img2.jpg"
  ]
}

Cart Object

{
  "cartId": "CART-123",
  "userId": "USER-456",
  "items": [
    {
      "productId": "PROD-001",
      "name": "Wireless Headphones",
      "quantity": 2,
      "unitPrice": 79.99,
      "subtotal": 159.98
    },
    {
      "productId": "PROD-002",
      "name": "Phone Case",
      "quantity": 1,
      "unitPrice": 19.99,
      "subtotal": 19.99
    }
  ],
  "subtotal": 179.97,
  "discount": 17.99,
  "tax": 14.40,
  "total": 176.38,
  "couponCode": "SAVE10"
}

Order Object

{
  "orderId": "ORD-789",
  "status": "shipped",
  "createdAt": "2024-01-15T10:30:00Z",
  "updatedAt": "2024-01-16T14:22:00Z",
  "customer": {
    "id": "USER-456",
    "email": "john@example.com",
    "name": "John Doe"
  },
  "shippingAddress": {
    "street": "123 Main St",
    "city": "San Francisco",
    "state": "CA",
    "zipCode": "94102",
    "country": "USA"
  },
  "items": [...],
  "payment": {
    "method": "card",
    "last4": "4242",
    "status": "captured"
  },
  "tracking": {
    "carrier": "FedEx",
    "number": "123456789",
    "url": "https://fedex.com/track/123456789"
  }
}

---

Navigating JSON in Postman

JSON Path Notation

Access nested values using dot notation:

// Given response:
{
  "product": {
    "name": "Headphones",
    "price": 79.99,
    "rating": {
      "average": 4.5
    }
  }
}

// Access paths:
product.name          → "Headphones"
product.price         → 79.99
product.rating.average → 4.5

Array Access

// Given response:
{
  "products": [
    { "name": "Phone", "price": 699 },
    { "name": "Tablet", "price": 499 },
    { "name": "Watch", "price": 299 }
  ]
}

// Access paths:
products[0]           → { "name": "Phone", "price": 699 }
products[0].name      → "Phone"
products[1].price     → 499
products.length       → 3

---

Viewing JSON in Postman

Pretty View

Click Pretty in the response body - Postman formats JSON with indentation.

Raw View

Click Raw to see the original unformatted response.

Preview View

For HTML responses, Preview renders the page.

Search in Response

Use Ctrl/Cmd + F to search within the response body.

---

JSON Validation

Common JSON Errors

Missing comma:
{
  "name": "Product"
  "price": 99
}

❌ Error: Missing comma after "Product"

Trailing comma:
{
  "name": "Product",
  "price": 99,
}

❌ Error: Trailing comma not allowed

Single quotes:
{
  'name': 'Product'
}

❌ Error: Must use double quotes

Unquoted key:
{
  name: "Product"
}

❌ Error: Keys must be quoted

Validating in Postman

Postman automatically validates JSON. If your request body has errors, you'll see a red indicator.

---

Sending JSON in Requests

POST with JSON Body

POST https://fakestoreapi.com/products
  • Go to Body tab
  • Select raw
  • Choose JSON from dropdown
  • Enter your JSON:
{
  "title": "New Product",
  "price": 49.99,
  "description": "A great product",
  "category": "electronics",
  "image": "https://example.com/image.jpg"
}

Postman automatically sets Content-Type: application/json

---

Working with Arrays

Array of Products Response

[
  { "id": 1, "name": "Phone", "price": 699 },
  { "id": 2, "name": "Tablet", "price": 499 },
  { "id": 3, "name": "Watch", "price": 299 }
]

Note: Response is an array [...], not an object {...}

Sending Array in Request

Adding multiple items to cart:

{
  "items": [
    { "productId": "PROD-001", "quantity": 2 },
    { "productId": "PROD-002", "quantity": 1 },
    { "productId": "PROD-003", "quantity": 3 }
  ]
}

---

Common JSON Patterns

Pagination Response

{
  "data": [
    { "id": 1, "name": "Product 1" },
    { "id": 2, "name": "Product 2" }
  ],
  "pagination": {
    "page": 1,
    "limit": 10,
    "total": 150,
    "totalPages": 15,
    "hasNext": true,
    "hasPrev": false
  }
}

Error Response

{
  "error": {
    "code": "INVALID_COUPON",
    "message": "The coupon code 'SAVE50' is not valid",
    "details": {
      "couponCode": "SAVE50",
      "reason": "expired"
    }
  }
}

Success with Message

{
  "success": true,
  "message": "Order placed successfully",
  "data": {
    "orderId": "ORD-789",
    "estimatedDelivery": "2024-01-20"
  }
}

---

Practice: Parse Complex JSON

Given this order response, identify the paths:

{
  "order": {
    "id": "ORD-100",
    "items": [
      {
        "product": { "name": "Laptop", "price": 999 },
        "quantity": 1
      },
      {
        "product": { "name": "Mouse", "price": 29 },
        "quantity": 2
      }
    ],
    "shipping": {
      "address": {
        "city": "New York"
      }
    }
  }
}
Find the paths for:
  • Order ID
  • First item's product name
  • Second item's quantity
  • Shipping city
Answers
  • order.id → "ORD-100"
  • order.items[0].product.name → "Laptop"
  • order.items[1].quantity → 2
  • order.shipping.address.city → "New York"

---

Key Takeaways

  • JSON uses key-value pairs with strict syntax rules
  • Access nested data with dot notation: object.property
  • Access arrays with index: array[0]
  • Always use double quotes for keys and strings
  • Postman formats and validates JSON automatically

Next up: Writing Tests & Assertions - automated validation of your API responses!

Request Parameters & Headers