JSON Assignment– 3

Basic Questions

  1. Use fetch() to get JSON from https://jsonplaceholder.typicode.com/posts/1 and print the title.
  2. Fetch JSON data from https://jsonplaceholder.typicode.com/users and print the first user’s name.
  3. Fetch JSON data from https://jsonplaceholder.typicode.com/todos/5 and print the completed status.
  4. Use fetch() and .then() to log all comments from https://jsonplaceholder.typicode.com/comments.
  5. Fetch posts from https://jsonplaceholder.typicode.com/posts and print the length of the array.
  6. Fetch albums data and log the id of the 3rd album.
  7. Fetch todos list and print only those with completed:true.
  8. Fetch user data and log the email of user with id=2.
  9. Use fetch() to get photos and print the first photo’s url.
  10. Fetch comments and count how many belong to postId=1.
  11. Use fetch() with .then() to convert response to JSON and print its type.
  12. Fetch posts and log titles of first 5 posts using a loop.
  13. Fetch a single post and print both id and body.
  14. Fetch JSON data and handle it inside .then() with console.log(Object.keys(data)).
  15. Fetch all todos and filter only those with userId=1.
  16. Fetch albums and print all titles that contain the word “accusamus”.
  17. Fetch users and log all usernames using forEach().
  18. Fetch JSON and access nested property address.city for first user.
  19. Fetch comments and map all emails into a new array.
  20. Fetch JSON and print typeof the returned data.

Intermediate Questions

  1. Fetch JSON using async/await and log first user’s username.
  2. Use async/await to fetch posts and print total number of posts.
  3. Write async function to fetch todos and filter those with completed:false.
  4. Use try…catch while fetching users API to handle possible error.
  5. Fetch posts using async/await and log only titles.
  6. Use Promise.all() to fetch posts and comments simultaneously.
  7. Fetch comments with async/await and count unique postId.
  8. Fetch photos and log first 10 URLs.
  9. Fetch todos and calculate how many are completed vs pending.
  10. Use catch() block in fetch() chain to log error message.
  11. Fetch posts with fetch() and intentionally use wrong URL to trigger error.
  12. Use try…catch with async/await to fetch a wrong API and print “Error occurred”.
  13. Fetch albums and display data length with .then() chain.
  14. Fetch users and extract all names into a single string separated by commas.
  15. Use map() on fetched posts to create array of only IDs.
  16. Fetch comments and reduce them to count total characters in all body.
  17. Use for…of loop on fetched todos to print each title.
  18. Use async/await and destructuring to extract name, email from first user.
  19. Use .finally() with fetch to log “Request completed”.
  20. Use nested fetch calls: first fetch users, then fetch todos of first user.

Advanced Level

  1. Save fetched post data into localStorage using localStorage.setItem().
  2. Retrieve JSON string from localStorage using getItem() and parse it back.
  3. Save an array of objects into localStorage after JSON.stringify().
  4. Retrieve array from localStorage, loop through, and log each object.
  5. Delete specific data from localStorage using removeItem().
  6. Clear entire localStorage using clear().
  7. Fetch JSON, store it in localStorage, then update one property and re-save it.
  8. Validate invalid JSON string with try…catch and print “Invalid JSON”.
  9. Use JSON.parse() on wrong format JSON and handle error gracefully.
  10. Write function saveAndGet() that saves object to localStorage as JSON and retrieves it back.