question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Inserting JSON Array produces error

See original GitHub issue

Hi all,

I’ve already looked at #94 and #864 and still cannot get this to work. I’ve also tried @ricardogama’s bookshelf-json-columns plugin with no luck.

As other’s have described, the issue is that an error is thrown when inserting a JSON Array into a JSONB column in Postgres. Inserting a JSON object works fine, just not an array of JSON objects. There is a known issue that you must stringify the input before passing it through node-pg – this is what the bookshelf-json-columns plugin does. Unfortunately, when an array of these objects is passed, it appears that Bookshelf is stringifying the input multiple times. Here’s an error when trying to insert a JSON array into a JSONB column:

[
   { serviceId: 2, petId: 5, checked: true },
   { serviceId: 3, petId: null, checked: false }
]

After stringifying:

"[{"serviceId":2,"petId":5,"checked":true},{"serviceId":3,"petId":null,"checked":false}]"

Insert error:

{"msec":54.03032702207565,
"error":"update \"appointment\" set \"ended_at\" = $1, \"report_card\" = $2, \"staff_notes\" = $3, \"status\" = $4, \"updated_at\" = $5 where \"id\" = $6 - invalid input syntax for type json","data":{"message":"update \"appointment\" set \"ended_at\" = $1, \"report_card\" = $2, \"staff_notes\" = $3, \"status\" = $4, \"updated_at\" = $5 where \"id\" = $6 - invalid input syntax for type json",
"severity":"ERROR",
"code":"22P02",
"condition":"invalid_text_representation",
"detail":"Expected \":\", but found \",\".","where":"JSON data, line 1: {\"{\\\"serviceId\\\":2,\\\"petId\\\":5,\\\"checked\\\":true}\",...","file":"json.c","line":1198,"routine":"report_parse_error","name":"PgError","isBoom":true,"isServer":true,"data":null,"output":{"statusCode":500,"payload":{"statusCode":500,"error":"Internal Server Error","message":"An internal server error occurred"},"headers":{}}}}

You’ll notice that the output detailed in the error message contains a number of extra slashes, which appear to be caused by stringifying the input multiple times.

However, if I do the following in Knex, I get no errors, which leads me to believe this is a Bookshelf issue:

    return knex.table('appointment').insert({
        business_id: businessId,
        staff_id: staffId,
        customer_id: customerId,
        report_card: JSON.stringify([
            { serviceName: 'Poop', 'petName': 'JJ', checked: true },
            { serviceName: 'Pee', 'petName': 'Mario', checked: true },
            { serviceName: 'Poop', 'petName': 'JJ', checked: true },
            { serviceName: 'Pee', 'petName': 'Mario', checked: true }
        ])
    });

I started digging through the Bookshelf code to find out where this is happening, but someone who knows the codebase better may be able to expedite things.

Thanks, James

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:3
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
jamesdixoncommented, Sep 19, 2016

@cjnqt use the plugin I mentioned above. @ricardogama just released a new version that resolves this issue.

0reactions
hashtdcommented, May 26, 2017

I got similar issue similar issue with following error code: '22P02', detail: 'Expected ":", but found "}".', hint: undefined, position: undefined, internalPosition: undefined, internalQuery: undefined, where: 'JSON data, line 1: {"{\\"l\\":\\"b\\"}"}', & using plugin bookshelf-json-columns

issue was model had initialize method

class MyModel extends Bookshelf.Model {
   initialize(attributes, options) {
       //my staff
   }
   static jsonColumns = ['foo', 'bar'];
}

after adding Bookshelf.Model.prototype.initialize.apply(this, arguments); in initialize method solved the issue

class MyModel extends Bookshelf.Model {
   initialize(attributes, options) {
       //after staff
       Bookshelf.Model.prototype.initialize.apply(this, arguments);
   }
   static jsonColumns = ['foo', 'bar'];
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

problem inserting json array into postgres: malformed error
I'm trying to insert an array of JSON values into a postgres database. But I'm getting this error: error: malformed array literal: ...
Read more >
Solve common issues with JSON in SQL Server
I want to produce complex JSON with several arrays on the same level. FOR JSON PATH can create nested objects using paths, and...
Read more >
39 JSON in Oracle Database
In JSON an array is represented by brackets ( [ , ] ) surrounding the representations of the array elements, which are separated...
Read more >
Working with JSON data in Google Standard SQL | BigQuery
You can then query the values of fields and array elements within the JSON data by using the field access operator, which makes...
Read more >
Importing CSV with a json array of strings in one of the column
Things I've tried and errors I got · ['a', 'b', 'c'] is not valid JSON, because strings must be enclosed in double quotes,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found