Saro Saro -5 years ago 171
JSON Question

What is the first line in the given JSON?

While Working with Sequelize in nodeJs, I received this error from Sequelize which looks like this:

{ [SequelizeUniqueConstraintError: Validation error]
  name: 'SequelizeUniqueConstraintError',
  message: 'Validation error',
   [ { message: 'unique_identifier_UNIQUE must be unique',
       type: 'unique violation',
       path: 'unique_identifier_UNIQUE',
       value: 'authentication123456' } ],
  fields: { unique_identifier_UNIQUE: 'authentication123456' } }

The error itself is very much clear. But what gained my attention is that this JSON looks weird.
In the first line of this JSON you can see:

[SequelizeUniqueConstraintError: Validation error]

Which is not string, nor array and even not followed by a comma to separate it from the next line in the JSON.

What is it then? It looks like a comment or title to me. But is this allowed in JSON? I did not find any reference explaining that.

Answer Source

Ok, I understood where this comes from.

this error was printed with console.log(err) command. What it does is that it calls the toString method of err object which formats the result as it is. Thanks to @Pointy I noticed that this is not even a JSON and that made me understand the issue. So instead of console.log(err), JSON.stringify(err) is called and now we have the correct jSON format:

    "name": "SequelizeUniqueConstraintError",
    "message": "Validation error",
    "errors": [
            "message": "unique_identifier_UNIQUE must be unique",
            "type": "unique violation",
            "path": "unique_identifier_UNIQUE",
            "value": "authentication123456"
    "fields": {
        "unique_identifier_UNIQUE": "authentication123456"
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download