Practice: JSONPlaceholder

About JSONPlaceholder

Free fake REST API for testing and prototyping. No auth required.

Available Resources

Resource Count Description

/posts

100

Blog posts

/comments

500

Comments on posts

/albums

100

Photo albums

/photos

5000

Photos in albums

/todos

200

Todo items

/users

10

Users

Level 1: Basic Requests

Exercise 1.1: Fetch All Users

# Your task: Fetch all users and pretty-print
curl -s https://jsonplaceholder.typicode.com/users | jq

Exercise 1.2: Fetch Single User

# Fetch user with ID 5
curl -s https://jsonplaceholder.typicode.com/users/5 | jq

Exercise 1.3: Extract Specific Field

# Get only user 3's email
curl -s https://jsonplaceholder.typicode.com/users/3 | jq -r '.email'

Exercise 1.4: List All Emails

# Extract all user emails
curl -s https://jsonplaceholder.typicode.com/users | jq -r '.[].email'

Level 2: Filtering and Selection

Exercise 2.1: Filter by Field

# Find users in "Gwenborough" city
curl -s https://jsonplaceholder.typicode.com/users | jq '.[] | select(.address.city == "Gwenborough")'

Exercise 2.2: Query Parameters

# Get posts by user 1 using query param
curl -s "https://jsonplaceholder.typicode.com/posts?userId=1" | jq

Exercise 2.3: Nested Resource

# Get comments for post 1
curl -s https://jsonplaceholder.typicode.com/posts/1/comments | jq

Exercise 2.4: Filter Completed Todos

# Find completed todos for user 1
curl -s "https://jsonplaceholder.typicode.com/todos?userId=1" | jq '[.[] | select(.completed == true)]'

Level 3: Data Transformation

Exercise 3.1: Build Custom Object

# Create simplified user objects
curl -s https://jsonplaceholder.typicode.com/users | jq '[.[] | {
  id,
  name,
  email,
  city: .address.city,
  company: .company.name
}]'

Exercise 3.2: Count Items

# How many posts does user 7 have?
curl -s "https://jsonplaceholder.typicode.com/posts?userId=7" | jq 'length'

Exercise 3.3: Sort and Limit

# Get first 5 users sorted by name
curl -s https://jsonplaceholder.typicode.com/users | jq 'sort_by(.name) | .[0:5] | .[].name'

Exercise 3.4: Group and Aggregate

# Count posts per user
curl -s https://jsonplaceholder.typicode.com/posts | jq 'group_by(.userId) | .[] | {userId: .[0].userId, post_count: length}'

Level 4: Write Operations

Exercise 4.1: Create Resource (POST)

# Create a new post (simulated - returns fake ID)
curl -s -X POST \
  -H "Content-Type: application/json" \
  -d '{"title": "My Post", "body": "Content here", "userId": 1}' \
  https://jsonplaceholder.typicode.com/posts | jq

Exercise 4.2: Update Resource (PUT)

# Replace post 1 entirely
curl -s -X PUT \
  -H "Content-Type: application/json" \
  -d '{"id": 1, "title": "Updated", "body": "New content", "userId": 1}' \
  https://jsonplaceholder.typicode.com/posts/1 | jq

Exercise 4.3: Partial Update (PATCH)

# Update just the title
curl -s -X PATCH \
  -H "Content-Type: application/json" \
  -d '{"title": "New Title Only"}' \
  https://jsonplaceholder.typicode.com/posts/1 | jq

Exercise 4.4: Delete Resource

# Delete post 1 (simulated)
curl -s -X DELETE https://jsonplaceholder.typicode.com/posts/1
# Returns empty object {} on success

Level 5: Advanced Challenges

Challenge 5.1: User Activity Summary

Create a summary showing each user’s name and their total posts, comments, and todos:

# Solution approach:
# 1. Fetch users
# 2. For each user, count their posts, todos
# 3. Build summary object

for id in $(seq 1 10); do
  name=$(curl -s "https://jsonplaceholder.typicode.com/users/$id" | jq -r '.name')
  posts=$(curl -s "https://jsonplaceholder.typicode.com/posts?userId=$id" | jq 'length')
  todos=$(curl -s "https://jsonplaceholder.typicode.com/todos?userId=$id" | jq 'length')
  echo "$name: $posts posts, $todos todos"
  sleep 0.2  # Be nice
done

Challenge 5.2: Find Most Active User

# Find user with most posts
curl -s https://jsonplaceholder.typicode.com/posts | jq '
  group_by(.userId)
  | map({userId: .[0].userId, count: length})
  | sort_by(.count)
  | reverse
  | .[0]
'

Challenge 5.3: Build CSV Export

# Export users to CSV
echo "id,name,email,city"
curl -s https://jsonplaceholder.typicode.com/users | jq -r '.[] | [.id, .name, .email, .address.city] | @csv'

Challenge 5.4: Cross-Resource Join

# Get posts with author names (manual join)
curl -s https://jsonplaceholder.typicode.com/posts | jq '.[0:3]' | while read -r post; do
  user_id=$(echo "$post" | jq -r '.userId')
  author=$(curl -s "https://jsonplaceholder.typicode.com/users/$user_id" | jq -r '.name')
  echo "$post" | jq --arg author "$author" '. + {author: $author}'
done

Shell Function

Add to your .zshrc:

jsonph() {
  local endpoint="${1:-posts}"
  local jq_filter="${2:-.}"

  curl -sS "https://jsonplaceholder.typicode.com/${endpoint}" | jq "$jq_filter"
}

# Usage:
# jsonph users
# jsonph posts/1
# jsonph "todos?userId=1" '.[] | select(.completed)'

Exercises Checklist

  • Fetch all users, extract just names

  • Get user 1’s geo coordinates

  • Count how many todos are completed across all users

  • Find the user with the longest company catchphrase

  • Create a post and capture the returned ID

  • Build a function that shows a user’s full profile (user + posts + todos)