Search code examples
jsonxqueryjsoniq

How to run JSONiq from JSON with try.zorba.io


I need to write a JSONiq expression that lists only the name of the products that cost at least 3. This is my JSON file which i had typed in the XQuery section:

{ "supermarket_visit":{
        "date":"08032019",
        "bought":[
            "item",{
                "type":"confectionary",
                "item_name":"Kit_Kat",
                "number": 3,
                "individual_price": 3.5
                },
            "item",{
                "type":"drinks",
                "item_name":"Coca_Cola",
                "number": 2,
                "individual_price": 3
                },
            "item",{
                "type":"fruits",
                "item_name":"apples",
                "number": "some"
                }
            ], 
"next_visit":[
            "item",{
                "type":"stationary",
                "item_name":"A4_paper",
                "number": 1
                },
            "item",{
                "type":"stationary",
                "item_name":"pen",
                "number": 2
                }
            ]
        }
}

and this is my JSONiq Xquery JSONiq command, which i dont really know where to type in try.zorba.io:

let $x := find("supermarket_visit")
for $x in $supermarket.bought let $i := $x.item
where $i.individual_price <=3
return $i.item_name

I am getting many errors in try.zorba.io and im really new to JSONiq and JSON. Is something wrong with my JSON or JSONiq part?


Solution

  • The following selection works for me at the site you linked to:

    jsoniq version "1.0";
    { "supermarket_visit":{
            "date":"08032019",
            "bought":[
                "item",{
                    "type":"confectionary",
                    "item_name":"Kit_Kat",
                    "number": 3,
                    "individual_price": 3.5
                    },
                "item",{
                    "type":"drinks",
                    "item_name":"Coca_Cola",
                    "number": 2,
                    "individual_price": 3
                    },
                "item",{
                    "type":"fruits",
                    "item_name":"apples",
                    "number": "some"
                    }
                ],
    "next_visit":[
                "item",{
                    "type":"stationary",
                    "item_name":"A4_paper",
                    "number": 1
                    },
                "item",{
                    "type":"stationary",
                    "item_name":"pen",
                    "number": 2
                    }
                ]
            }
    }.supermarket_visit.bought()[$$ instance of object and $$.individual_price le 3].item_name