JSON Assignment- 2
Basic Questions
- Parse this JSON string in JavaScript:
{“id”:101,”name”:”Alice”,”age”:22} and print the name. - Convert this object to JSON using JSON.stringify():
{ product:”Laptop”, price:45000, available:true } - Parse a JSON array:
[“Red”,”Blue”,”Green”] and print the second element. - Convert this array to a JSON string:
[“JavaScript”,”Python”,”C++”] - Use JSON.parse() on {“marks”:[10,20,30]} and print the sum of marks.
- Stringify an object:
{id:1, title:”Book”, author:”John”} and print the JSON string. - Parse {“status”:”success”,”code”:200} and access the code value.
- Convert {x:10,y:20,z:30} to JSON string and check its type.
- Parse this JSON string {“user”:{“id”:1,”name”:”Sarthak”}} and print name.
- Convert an object with boolean value to JSON:
{ isAdmin:true }. - Parse an array JSON: {“fruits”:[“apple”,”banana”,”cherry”]} and print first fruit.
- Stringify nested object {car:{brand:”BMW”,year:2022}}.
- Parse {“scores”:[45,67,89]} and get the highest score.
- Stringify { language:”JS”, framework:”React” }.
- Parse {“price”:1000,”discount”:200} and print final price.
- Convert [{id:1,name:”Dev”},{id:2,name:”Riya”}] to JSON string.
- Parse {“login”:true,”user”:”guest”} and print login status.
- Stringify { country:”India”, capital:”Delhi” }.
- Parse {“skills”:[“HTML”,”CSS”,”JS”]} and loop through skills with for.
- Convert { topic:”JSON”, level:”basic” } to JSON string and print length.
Intermediate Questions
- Parse {“employee”:{“id”:101,”dept”:”HR”}} and access dept using dot notation.
- Parse same JSON using bracket notation and access dept.
- Parse {“students”:[{“id”:1,”name”:”A”},{“id”:2,”name”:”B”}]} and print all names.
- Stringify an object with nested array { project:”App”, tasks:[“UI”,”Backend”] }.
- Parse {“numbers”:[1,2,3,4,5]} and double each number using map().
- Parse {“names”:[“Sam”,”Tom”,”Max”]} and print each with forEach().
- Parse {“grades”:[90,80,85]} and calculate average with for…of.
- Stringify array of objects:
[{id:1,item:”Pen”},{id:2,item:”Pencil”}]. - Parse {“details”:{“a”:1,”b”:2,”c”:3}} and iterate keys with for…in.
- Parse {“fruits”:[“apple”,”mango”]} and add a new fruit “orange”.
- Stringify object { title:”Course”, students:[1,2,3] }.
- Parse {“user”:{“name”:”John”,”contacts”:[“123″,”456”]}} and print contacts.
- Parse {“items”:[{“id”:1,”qty”:2},{“id”:2,”qty”:5}]} and sum qty.
- Convert [{city:”NY”},{city:”LA”}] to JSON string and parse back to array.
- Parse {“marks”:{“math”:90,”science”:80}} and iterate with Object.keys().
- Parse {“marks”:{“math”:90,”science”:80}} and iterate with Object.values().
- Parse {“marks”:{“math”:90,”science”:80}} and iterate with Object.entries().
- Parse {“orders”:[{“id”:1,”amount”:500},{“id”:2,”amount”:1000}]} and find total.
- Stringify object {id:5,active:false,data:[10,20,30]}.
- Parse {“colors”:[“red”,”blue”,”green”]} and filter only “blue”.
Advanced Questions
- Write a try…catch block to safely parse a correct JSON string.
- Write a try…catch block to safely parse an incorrect JSON string.
- Parse {“data”:[{“x”:10},{“x”:20},{“x”:30}]} and calculate sum of all x.
- Parse deeply nested JSON {“a”:{“b”:{“c”:100}}} and access c.
- Stringify an object with function property {id:1, greet:()=>{}} and see result.
- Parse {“numbers”:[5,10,15,20]} and filter numbers > 10.
- Parse {“users”:[{“id”:1},{“id”:2},{“id”:3}]} and convert into array of ids.
- Parse {“matrix”:[[1,2],[3,4],[5,6]]} and flatten the array.
- Parse {“cart”:[{“item”:”Book”,”price”:200},{“item”:”Pen”,”price”:50}]} and compute total price.
- Parse {“departments”:[{“name”:”IT”,”employees”:[{“id”:1},{“id”:2}]}]} and count employees in IT.