1

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'
Ghislain Fourny
  • 6,971
  • 1
  • 30
  • 37
KeKDee
  • 43
  • 5
  • This is not legal JSON: `{ {`, you can't have unnamed objects inside other objects, you need a key for the inner object, like `{ "key": {`, and you have similar issues all through your declaration here. – Lasse V. Karlsen Aug 12 '21 at 07:13

1 Answers1

1

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.

Ghislain Fourny
  • 6,971
  • 1
  • 30
  • 37
  • Hey Ghislain! Just want to say thanks for taking your time for answering my problems, may I ask which part of the RumbleDB website did you test this one, I could not find it would like to try it – KeKDee Aug 14 '21 at 06:07
  • 1
    Of course: I tried it with the public notebook: https://colab.research.google.com/github/RumbleDB/rumble/blob/master/RumbleSandbox.ipynb You can also download and install RumbleDB on your machine. – Ghislain Fourny Aug 16 '21 at 12:12