JSON Assignment- 4
Basic Questions
- Given JSON { “name”:”Alice”,”age”:25 }, destructure name into a variable.
- Destructure array [10,20,30] from JSON and print first element.
- Destructure nested JSON { “student”:{ “id”:1, “course”:”Math” }} to extract course.
- Merge two JSON objects {a:1,b:2} and {c:3} using spread operator.
- Combine two arrays [1,2] and [3,4] using spread operator.
- Destructure array [ “red”,”blue”,”green” ] to get first two colors.
- Use rest operator to capture remaining elements from [1,2,3,4,5].
- Destructure nested array [ [1,2], [3,4] ] to extract inner arrays.
- Spread operator: copy object {id:1,name:”Tom”} into a new object.
- Spread operator: copy array [100,200,300] into new array and add 400.
- Given JSON {x:5,y:10,z:15}, destructure x and z.
- Destructure object {user:{name:”Dev”, age:22}} to extract name.
- Rest operator: collect leftover keys from object {a:1,b:2,c:3} into new object.
- Use spread to combine two user objects into one.
- Destructure [ {id:1}, {id:2} ] and access id of first element.
- Spread operator: clone object and override property {id:1,name:”A”} to name “B”.
- Destructure [10,20,30,40] with first value and rest operator.
- Given JSON { “book”:{ “title”:”JS”,”author”:”John” }}, destructure author.
- Destructure [ “apple”,”banana”,”cherry” ] to extract only “banana”.
- Merge multiple arrays [1,2], [3,4], [5,6] with spread operator.
Intermediate Questions
- Destructure nested JSON:
{ user:{ profile:{ name:”Sam”, city:”NY” }}} → extract city. - Destructure object with alias:
{id:10, username:”dev”} → extract username as variable uname. - Destructure JSON:
{person:{details:{age:30,skills:[“JS”,”React”]}}} → extract skills. - Spread operator: merge array [1,2] with […numbers, 3,4].
- Combine two objects {a:1} and {b:2} and add {c:3} inline with spread.
- Destructure array [100,200,300,400] into first, second, and rest values.
- Spread operator: shallow copy JSON object and modify nested key.
- Rest operator: function parameter (…args) with destructured array.
- Destructure JSON array:
[{id:1,name:”A”},{id:2,name:”B”}] → extract name of second user. - Spread operator: create new array by spreading [1,2,3] and adding [4,5].
- Nested destructuring:
{data:{info:{values:[1,2,3]}}} → extract 2. - Use rest operator in object destructuring: {x:1,y:2,z:3} → take x and collect rest.
- Spread operator: merge default settings {theme:”dark”} with user settings {font:”large”}.
- Destructure [ {title:”Book1″}, {title:”Book2″} ] → extract second title.
- Nested destructuring array: [ [1,2], [3,4] ] → extract 3.
- Spread operator: flatten array [ [1,2], [3,4] ] into [1,2,3,4].
- Destructure deeply:
{company:{employees:[{id:1,role:”dev”}]}} → extract role. - Rest operator: [10,20,30,40] → extract 10 and collect rest into new array.
- Spread operator: clone nested array [ [1,2],[3,4] ].
- Combine 3 objects {a:1}, {b:2}, {c:3} with spread.
Advanced Questions
- Validate JSON string {“id”:1,”name”:”Sam”} using JSON Schema rules (id → number, name → string).
- Create schema to validate object {product:”Book”,price:200}.
- Given invalid JSON string {“name”:”John”,age:30} (missing quotes), handle error using try/catch.
- Convert JSON array [{“id”:1,”name”:”A”},{“id”:2,”name”:”B”}] into CSV format.
- Convert object {x:1,y:2} into XML format string.
- Given deeply nested JSON {a:{b:{c:{d:100}}}}, access value of d.
- Write function to flatten JSON {a:1,b:{c:2,d:{e:3}}} into {a:1,c:2,e:3}.
- Given JSON array [ [1,2], [3,4], [5,6] ], flatten into [1,2,3,4,5,6].
- Use an online JSON beautifier (programmatically) to pretty print JSON string.
- Use JSON minifier logic to remove spaces/newlines from:
{
"id":1,
"name":"Test"
}