So I have written a JSON file code to be able to help display the title of books but when I compile I kept getting this same error, is there a way for the fix or am I doing something wrong?

The code is below:

jsoniq version "1.0";
let $file: = {
    {
        "title": "Fifty Shades of Grey",
        "author": "E.L.",
        "date_read": {
            "month": "May",
            "year": "2016"
        },
        "opinion": "Did not like very much"
    },
    {
        "title": "The grass is singing",
        "author": "Doris Lessing",
        "date_read": {
            "month": "June",
            "year": "2016"
        },
        "opinion": "Enjoyed quite a bit"
    },
    {
        "title": "A short history on nearly everything",
        "author": "Bill Bryson",
        "date_read": {
            "month": "July",
            "year": "2016"
        },
        "opinion": "Very informative"
    },
    {
        "title": "JSON in 24 hours",
        "author": "Peter Settler",
        "purpose": "Work"
    },
    {}
}
for $x in $file
return $x.title

Error that has been displayed:

Error: Parse error on line 1:
jsoniq version "1.0"
^
Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '[', got 'undefined'
1

There are 1 best solutions below

2
Ghislain Fourny On

There are two issues with the JSONiq code:

  • the space between : and = after $file
  • (...) should be used to create a sequence of objects, and not {...} as {...} is for creating JSON objects (as lists of key-value pairs).

I hope this helps! This query should work (I tested it with RumbleDB):

jsoniq version "1.0";
let $file := (
  { "title": "Fifty Shades of Grey", "author": "E.L.", "date_read": { "month": "May", "year": "2016" }, "opinion": "Did not like very much" },
  { "title": "The grass is singing", "author": "Doris Lessing", "date_read": { "month": "June", "year": "2016" }, "opinion": "Enjoyed quite a bit" },
  { "title": "A short history on nearly everything", "author": "Bill Bryson", "date_read": { "month": "July", "year": "2016" }, "opinion": "Very informative" },
  { "title": "JSON in 24 hours", "author": "Peter Settler", "purpose": "Work" },
  {}
)
for $x in $file
return $x.title

Another thought: the error message you are getting may indicate that you are trying to feed this code into a JSON parser and not into a JSONiq engine. Syntactically, JSON is a subset of JSONiq, so you can feed JSON into any JSONiq engine and it will "return itself". However, JSONiq is not a subset of JSON so it does not work the other way around.