JSON Assignment- 2

Basic Questions

  1. Parse this JSON string in JavaScript:
    {“id”:101,”name”:”Alice”,”age”:22} and print the name.
  2. Convert this object to JSON using JSON.stringify():
    { product:”Laptop”, price:45000, available:true }
  3. Parse a JSON array:
    [“Red”,”Blue”,”Green”] and print the second element.
  4. Convert this array to a JSON string:
    [“JavaScript”,”Python”,”C++”]
  5. Use JSON.parse() on {“marks”:[10,20,30]} and print the sum of marks.
  6. Stringify an object:
    {id:1, title:”Book”, author:”John”} and print the JSON string.
  7. Parse {“status”:”success”,”code”:200} and access the code value.
  8. Convert {x:10,y:20,z:30} to JSON string and check its type.
  9. Parse this JSON string {“user”:{“id”:1,”name”:”Sarthak”}} and print name.
  10. Convert an object with boolean value to JSON:
    { isAdmin:true }.
  11. Parse an array JSON: {“fruits”:[“apple”,”banana”,”cherry”]} and print first fruit.
  12. Stringify nested object {car:{brand:”BMW”,year:2022}}.
  13. Parse {“scores”:[45,67,89]} and get the highest score.
  14. Stringify { language:”JS”, framework:”React” }.
  15. Parse {“price”:1000,”discount”:200} and print final price.
  16. Convert [{id:1,name:”Dev”},{id:2,name:”Riya”}] to JSON string.
  17. Parse {“login”:true,”user”:”guest”} and print login status.
  18. Stringify { country:”India”, capital:”Delhi” }.
  19. Parse {“skills”:[“HTML”,”CSS”,”JS”]} and loop through skills with for.
  20. Convert { topic:”JSON”, level:”basic” } to JSON string and print length.

Intermediate Questions

  1. Parse {“employee”:{“id”:101,”dept”:”HR”}} and access dept using dot notation.
  2. Parse same JSON using bracket notation and access dept.
  3. Parse {“students”:[{“id”:1,”name”:”A”},{“id”:2,”name”:”B”}]} and print all names.
  4. Stringify an object with nested array { project:”App”, tasks:[“UI”,”Backend”] }.
  5. Parse {“numbers”:[1,2,3,4,5]} and double each number using map().
  6. Parse {“names”:[“Sam”,”Tom”,”Max”]} and print each with forEach().
  7. Parse {“grades”:[90,80,85]} and calculate average with for…of.
  8. Stringify array of objects:
    [{id:1,item:”Pen”},{id:2,item:”Pencil”}].
  9. Parse {“details”:{“a”:1,”b”:2,”c”:3}} and iterate keys with for…in.
  10. Parse {“fruits”:[“apple”,”mango”]} and add a new fruit “orange”.
  11. Stringify object { title:”Course”, students:[1,2,3] }.
  12. Parse {“user”:{“name”:”John”,”contacts”:[“123″,”456”]}} and print contacts.
  13. Parse {“items”:[{“id”:1,”qty”:2},{“id”:2,”qty”:5}]} and sum qty.
  14. Convert [{city:”NY”},{city:”LA”}] to JSON string and parse back to array.
  15. Parse {“marks”:{“math”:90,”science”:80}} and iterate with Object.keys().
  16. Parse {“marks”:{“math”:90,”science”:80}} and iterate with Object.values().
  17. Parse {“marks”:{“math”:90,”science”:80}} and iterate with Object.entries().
  18. Parse {“orders”:[{“id”:1,”amount”:500},{“id”:2,”amount”:1000}]} and find total.
  19. Stringify object {id:5,active:false,data:[10,20,30]}.
  20. Parse {“colors”:[“red”,”blue”,”green”]} and filter only “blue”.

Advanced Questions

  1. Write a try…catch block to safely parse a correct JSON string.
  2. Write a try…catch block to safely parse an incorrect JSON string.
  3. Parse {“data”:[{“x”:10},{“x”:20},{“x”:30}]} and calculate sum of all x.
  4. Parse deeply nested JSON {“a”:{“b”:{“c”:100}}} and access c.
  5. Stringify an object with function property {id:1, greet:()=>{}} and see result.
  6. Parse {“numbers”:[5,10,15,20]} and filter numbers > 10.
  7. Parse {“users”:[{“id”:1},{“id”:2},{“id”:3}]} and convert into array of ids.
  8. Parse {“matrix”:[[1,2],[3,4],[5,6]]} and flatten the array.
  9. Parse {“cart”:[{“item”:”Book”,”price”:200},{“item”:”Pen”,”price”:50}]} and compute total price.
  10. Parse {“departments”:[{“name”:”IT”,”employees”:[{“id”:1},{“id”:2}]}]} and count employees in IT.