JSON Assignment– 3
Basic Questions
- Use fetch() to get JSON from https://jsonplaceholder.typicode.com/posts/1 and print the title.
- Fetch JSON data from https://jsonplaceholder.typicode.com/users and print the first user’s name.
- Fetch JSON data from https://jsonplaceholder.typicode.com/todos/5 and print the completed status.
- Use fetch() and .then() to log all comments from https://jsonplaceholder.typicode.com/comments.
- Fetch posts from https://jsonplaceholder.typicode.com/posts and print the length of the array.
- Fetch albums data and log the id of the 3rd album.
- Fetch todos list and print only those with completed:true.
- Fetch user data and log the email of user with id=2.
- Use fetch() to get photos and print the first photo’s url.
- Fetch comments and count how many belong to postId=1.
- Use fetch() with .then() to convert response to JSON and print its type.
- Fetch posts and log titles of first 5 posts using a loop.
- Fetch a single post and print both id and body.
- Fetch JSON data and handle it inside .then() with console.log(Object.keys(data)).
- Fetch all todos and filter only those with userId=1.
- Fetch albums and print all titles that contain the word “accusamus”.
- Fetch users and log all usernames using forEach().
- Fetch JSON and access nested property address.city for first user.
- Fetch comments and map all emails into a new array.
- Fetch JSON and print typeof the returned data.
Intermediate Questions
- Fetch JSON using async/await and log first user’s username.
- Use async/await to fetch posts and print total number of posts.
- Write async function to fetch todos and filter those with completed:false.
- Use try…catch while fetching users API to handle possible error.
- Fetch posts using async/await and log only titles.
- Use Promise.all() to fetch posts and comments simultaneously.
- Fetch comments with async/await and count unique postId.
- Fetch photos and log first 10 URLs.
- Fetch todos and calculate how many are completed vs pending.
- Use catch() block in fetch() chain to log error message.
- Fetch posts with fetch() and intentionally use wrong URL to trigger error.
- Use try…catch with async/await to fetch a wrong API and print “Error occurred”.
- Fetch albums and display data length with .then() chain.
- Fetch users and extract all names into a single string separated by commas.
- Use map() on fetched posts to create array of only IDs.
- Fetch comments and reduce them to count total characters in all body.
- Use for…of loop on fetched todos to print each title.
- Use async/await and destructuring to extract name, email from first user.
- Use .finally() with fetch to log “Request completed”.
- Use nested fetch calls: first fetch users, then fetch todos of first user.
Advanced Level
- Save fetched post data into localStorage using localStorage.setItem().
- Retrieve JSON string from localStorage using getItem() and parse it back.
- Save an array of objects into localStorage after JSON.stringify().
- Retrieve array from localStorage, loop through, and log each object.
- Delete specific data from localStorage using removeItem().
- Clear entire localStorage using clear().
- Fetch JSON, store it in localStorage, then update one property and re-save it.
- Validate invalid JSON string with try…catch and print “Invalid JSON”.
- Use JSON.parse() on wrong format JSON and handle error gracefully.
- Write function saveAndGet() that saves object to localStorage as JSON and retrieves it back.