Click here to Skip to main content
15,881,089 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
const user = [
{
    "userid" : "1",
    "emailid" : "xyz@gmail.com",
    "password" : "xyz123",
    "cards" : [
        {
            "parentUId" : "1",
            "cardId" : "1A",
            "cardTitle" : "Buy Vegetables",
            "tasks" : [{
                "parentCId" : "1A",
                "taskId" : "1A1",
                "taskTitle" : "Tomato",
                "isCompleted" : false
            }, {
                "parentCId" : "1A",
                "taskId" : "1A2",
                "taskTitle" : "Onion",
                "isCompleted" : false
            }]
        },
        {
            "parentUId" : "1",
            "cardId" : "1B",
            "cardTitle" : "Buy Fruits",
            "tasks" : [{
                "parentCId" : "1B",
                "taskId" : "1B1",
                "taskTitle" : "Mango",
                "isCompleted" : false
            }, {
                "parentCId" : "1B",
                "taskId" : "1B2",
                "taskTitle" : "Apple",
                "isCompleted" : false
            }]
        }
    ]
},
{
    "userid" : "2",
    "emailid" : "abc@gmail.com",
    "password" : "abc123",
    "cards" : [
        {
            "parentUId" : "1",
            "cardId" : "2A",
            "cardTitle" : "Buy Vegetables",
            "tasks" : [{
                "parentCId" : "2A",
                "taskId" : "2A1",
                "taskTitle" : "Tomato",
                "isCompleted" : false
            }, {
                "parentCId" : "2A",
                "taskId" : "2A2",
                "taskTitle" : "Onion",
                "isCompleted" : false
            }]
        }
    ]
}
]

let uid = "2"
let cdTitle = "Buy Vegetables"


console.log(user[userid = uid].cards[cardTitle = cdTitle].tasks)


What I have tried:

It is showing error.
How to ask queries in json.
Any Good examples and exercises will be appreciated
Posted
Updated 6-Jan-23 1:25am

You probably need to convert the JSON to Javascript objects. See JavaScript JSON[^].
 
Share this answer
 
Would suggest you too use an existing library that supports easy query into JSON data.

Couple of them:
NPM . json-query[^]
Simple Queries · JSONata[^]

Both the url's above have documentation/examples that will help you understand their usage.


With JSON-Query, following are the examples:
Queries are strings that describe an object or value to pluck out, or manipulate from the context object. The syntax is a little bit CSS, a little bit JS, but pretty powerful.
Accessing properties (dot notation)

person.name
Array accessors

people[0]
Array pluck

people.name => return all the names of people
Get all values of a lookup

lookup[*]
Array filter

By default only the first matching item will be returned:

people[name=Matt]

But if you add an asterisk (*), all matching items will be returned:

people[*country=NZ]

You can use comparative operators:

people[*rating>=3]

Or use boolean logic:

people[* rating >= 3 & starred = true]

If options.enableRegexp is enabled, you can use the ~ operator to match RegExp:

people[*name~/^R/i]

You can also negate any of the above examples by adding a ! before the = or ~:

people[*country!=NZ]
Or syntax

person.greetingName|person.name
 
Share this answer
 
Comments
shhhuuu 5-Jan-23 5:59am    
Let cdTitle = "Buy Fruits", uid = "1"

Tell me query statement to log all the tasks of card containing cardTitle as cdTitle whose userid is uid.
Quote:
JavaScript
user[userid = uid].cards[cardTitle = cdTitle].tasks
Firstly, the array indexer expects the index of the item to return. It does not accept an expression to match a specific item.

Secondly, you are using the assignment operator (=) to copy the existing variables (uid and cdTitle) to new previously-undefined variables (userid and cardTitle). That's not going to filter the items in any way.

You'll need to use a combination of filter[^] and flatMap[^] to perform your query. You'll also need to use optional chaining[^] to avoid an error if the item you're searching for doesn't exist.
JavaScript
const tasks = user.filter(u => u.userid === uid)?.flatMap(u => u.cards.filter(c => c.cardTitle === cdTitle))?.flatMap(c => c.tasks);
console.log(tasks);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900