JSON Assignment- 4

Basic Questions

  1. Given JSON { “name”:”Alice”,”age”:25 }, destructure name into a variable.
  2. Destructure array [10,20,30] from JSON and print first element.
  3. Destructure nested JSON { “student”:{ “id”:1, “course”:”Math” }} to extract course.
  4. Merge two JSON objects {a:1,b:2} and {c:3} using spread operator.
  5. Combine two arrays [1,2] and [3,4] using spread operator.
  6. Destructure array [ “red”,”blue”,”green” ] to get first two colors.
  7. Use rest operator to capture remaining elements from [1,2,3,4,5].
  8. Destructure nested array [ [1,2], [3,4] ] to extract inner arrays.
  9. Spread operator: copy object {id:1,name:”Tom”} into a new object.
  10. Spread operator: copy array [100,200,300] into new array and add 400.
  11. Given JSON {x:5,y:10,z:15}, destructure x and z.
  12. Destructure object {user:{name:”Dev”, age:22}} to extract name.
  13. Rest operator: collect leftover keys from object {a:1,b:2,c:3} into new object.
  14. Use spread to combine two user objects into one.
  15. Destructure [ {id:1}, {id:2} ] and access id of first element.
  16. Spread operator: clone object and override property {id:1,name:”A”} to name “B”.
  17. Destructure [10,20,30,40] with first value and rest operator.
  18. Given JSON { “book”:{ “title”:”JS”,”author”:”John” }}, destructure author.
  19. Destructure [ “apple”,”banana”,”cherry” ] to extract only “banana”.
  20. Merge multiple arrays [1,2], [3,4], [5,6] with spread operator.

Intermediate Questions

  1. Destructure nested JSON:
    { user:{ profile:{ name:”Sam”, city:”NY” }}} → extract city.
  2. Destructure object with alias:
    {id:10, username:”dev”} → extract username as variable uname.
  3. Destructure JSON:
    {person:{details:{age:30,skills:[“JS”,”React”]}}} → extract skills.
  4. Spread operator: merge array [1,2] with […numbers, 3,4].
  5. Combine two objects {a:1} and {b:2} and add {c:3} inline with spread.
  6. Destructure array [100,200,300,400] into first, second, and rest values.
  7. Spread operator: shallow copy JSON object and modify nested key.
  8. Rest operator: function parameter (…args) with destructured array.
  9. Destructure JSON array:
    [{id:1,name:”A”},{id:2,name:”B”}] → extract name of second user.
  10. Spread operator: create new array by spreading [1,2,3] and adding [4,5].
  11. Nested destructuring:
    {data:{info:{values:[1,2,3]}}} → extract 2.
  12. Use rest operator in object destructuring: {x:1,y:2,z:3} → take x and collect rest.
  13. Spread operator: merge default settings {theme:”dark”} with user settings {font:”large”}.
  14. Destructure [ {title:”Book1″}, {title:”Book2″} ] → extract second title.
  15. Nested destructuring array: [ [1,2], [3,4] ] → extract 3.
  16. Spread operator: flatten array [ [1,2], [3,4] ] into [1,2,3,4].
  17. Destructure deeply:
    {company:{employees:[{id:1,role:”dev”}]}} → extract role.
  18. Rest operator: [10,20,30,40] → extract 10 and collect rest into new array.
  19. Spread operator: clone nested array [ [1,2],[3,4] ].
  20. Combine 3 objects {a:1}, {b:2}, {c:3} with spread.

Advanced Questions

  1. Validate JSON string {“id”:1,”name”:”Sam”} using JSON Schema rules (id → number, name → string).
  2. Create schema to validate object {product:”Book”,price:200}.
  3. Given invalid JSON string {“name”:”John”,age:30} (missing quotes), handle error using try/catch.
  4. Convert JSON array [{“id”:1,”name”:”A”},{“id”:2,”name”:”B”}] into CSV format.
  5. Convert object {x:1,y:2} into XML format string.
  6. Given deeply nested JSON {a:{b:{c:{d:100}}}}, access value of d.
  7. Write function to flatten JSON {a:1,b:{c:2,d:{e:3}}} into {a:1,c:2,e:3}.
  8. Given JSON array [ [1,2], [3,4], [5,6] ], flatten into [1,2,3,4,5,6].
  9. Use an online JSON beautifier (programmatically) to pretty print JSON string.
  10. Use JSON minifier logic to remove spaces/newlines from:
     {
     "id":1,
     "name":"Test"
                  }