Home > Blockchain >  Data to be extracted from JSON like (JSONobj.name.fname) here (name.fname) is given in form of strin
Data to be extracted from JSON like (JSONobj.name.fname) here (name.fname) is given in form of strin

Time:09-29

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.Nameit 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.

CodePudding user response:

You can't use directly the array values in the object (like this: obj.array_values), they both have different types.

Try this one! It works fine.

function jsonfn(text, queries){
    let myObj = JSON.parse(text);
    for(let i = 0; i< queries.length ; i  )
    {
        let query = queries[i];
        if (Array.isArray(query)) {
          console.log(myObj[query[0]][query[1]]);
        }
        else {
          console.log(myObj[query]);
        }
    }
}

let JSONtext = `{"Student":{"Name":"John","Age":15},"Class":10}`;
let queries=[["Student", "Name"], ["Student", "Age"], "Class"];

jsonfn(JSONtext, queries)

CodePudding user response:

Thanks @siddharthRastogi I just changed your ans a bit, and got this!

let JSONtext = `{
    "Student": {
        "Name": "John",
        "Age": "15"
    },
    "Class": "10",
    "Address": {
        "Country": {
            "State": {
                "City": {
                    "location": "12, abc road",
                    "pincode": "987654"
                }
            }
        }
    }
}`;
let queries=["Student.Name","Student.Age", "Class" , "Address.Country.State.City.location","Address.Country.State.City.pincode"];
queries = changeQueryArr(queries);

Added a function to changeQueryArr() to convert queries into dot splitted queries for deeply nested objects.

function changeQueryArr(queries)
{
    let ans = []
    for(let i=0;i<queries.length;i  )
    {
        let str = queries[i];
        ans.push(str.split("."));
    }
    return ans;
}
now queries would look like

// queries=[["Student", "Name"], ["Student", "Age"], "Class",["Address","Country","State","City","location"],["Address","Country","State","City","pincode"]];



function jsonfn(text, queries){
    let myObj = JSON.parse(text);
    for(let i = 0; i< queries.length ; i  )
    {
        let query = queries[i];
        if (Array.isArray(query)) {
            console.log( query.reduce((prev, curr) => prev?.[curr], myObj))
        }
        else {
          console.log(myObj[query]);
        }
    }
}


jsonfn(JSONtext, queries)


// Ans :- 
// John
// 15
// 10
// 12, abc road
// 987654

  • Related