Search code examples
javascriptjsonjsonpath

Is it possible to return a child node and a parent node with JSON PATH in the same queries in javascript


Suppose that we have this JSON:

{
  "table" : 
    [
        {
            "A": "A1",
            "B":
            {  
                "BA":"BA1",
                "BB":"BB1"
            }
        },
        {
            "A": "A2",
            "B":
            {  
                "BA":"BA2",
                "BB":"BB2"
            }
        }
    ]
}

With ONLY ONE query I want to return a set of data composed by 2 type of data: "A" and "BA". For example I can make a request : $.table[<filter>].[A,B.BA] it doesn't work, but it's the idea. I want to return the value of the parent A and the child BA in ONLY one query. Firstly is it possible and secondly if yes, how ?


Solution

  • It seem that's impossible to retrieve precisely multiple attribute from a JSON Path request. But in certain case, we can do it... But these case are rare and it's risky to use this type of request. SO there is another way to query JSON objet: JMESPATH. It's FAR better and simplier, and most importantly it solve the problem of retrieving multiple attribute, for my problem the syntax would be: table[<filter>].[A,B.BA] (as I wanted in the initial post). Here's some links: - install it: npm page - tutorial/official website: website