A text in JSON format is given in the form of a string, and Queries is given consisting of key of given JSON object in the form of a string. For each query, value of that key is to be printed.
JSONtext = `{"Student":{"Name":"John","Age":"15"},"Class":"10"}`;
NoOfQueries=3;
queries=["Student.Name","Student.Age", "Class"];
ansShouldBe = John 15 10
queries[0] = ("Student.Name")
ans
should be calculated ans = myobj.Student.Name
is "John",
similarly for 2nd is 15 & 3rd is 10
I have written just the beginning of the code, can someone help me to complete this?
function jsonfn(text,N,queries){
let myObj = JSON.parse(text);
for(let i=0; i<queries.length ; i++)
{
let query = queries[i]; //student.name
console.log(myObj.query) //this is giving undefined
// please complete this code
}
}
If I run myObj.Student.Name
it gives ans
as "John" but how to do it dynamically if Student.Name is given in a string provided by user. How to parse Json object at run time? is there a way we can do it
The object can also be deeply nested like
{
"c": {
"mf": {
"he": "k"
}
},
"p": "q"
}
Same with the queries
["c.mf.he", "p"]
Please someone help me on this.