1

I have a table with two columns application and projects. While application is a simple column of varchar type, projects should hold array of jsonb objects.

A sample record is:

application | projects
-----------------------
App1        | [{"project": "project1", "url":"url1"},{"project": "project2", "url": "url2"}]

Now there is a json file which holds the data to be inserted into this table programmatically in the exact same structure as this table which needs to be read through Node.js and inserted into the table.

My question is how do I write an insert query which inserts the projects field correctly into the table. I am a beginner in Postgresql and unaware of the function that can do this. I want to write something like this:

Insert into <tablename> values('App1', 
some_function_that_converts_string_to_jsonb_array('[{"project": "project1", "url":"url1"},
{"project": "project2", "url": "url2"}]');

What function of Postgresql can achieve this? I have tried using jsonb_build_array but it is not working.

Thanks!

EDIT:

The value of the projects column should be that I can query on it.

4
  • It should work with just the string representation of the JSON: . . . values ('App1', '[1, 2, 3]') Commented Jul 26, 2023 at 12:36
  • I tried it. It is throwing error. "[" must introduce explicitly-specified array dimensions.. The type of projects column is jsonb[] Commented Jul 26, 2023 at 12:40
  • Querying can be done, but the syntax will be verbose. Inserting a new project will be problematic. If you are in the planning stages, then you should use a normalized schema to simplify those expected future operations. This is the classic case of "pay me now or pay me later." Taking the json shortcut with the one-time, up-front data modeling means you will pay dearly on every query, insert, or update you write in the future. Commented Jul 26, 2023 at 12:45
  • 2
    I still say to use a normalized schema, but to get past your error about "explicitly-specified array dimensions', change the type of the projects column to jsonb. Please see: stackoverflow.com/a/62917486/13808319 Commented Jul 26, 2023 at 12:46

1 Answer 1

0

If I understand you correct you want to insert a JSON as a string into a postgres table column of the type jsonb?

Then this is your query:

INSERT INTO <YOUR_TABLE>(projects) VALUES ('[{"project": "project1", "url":"url1"},{"project": "project2", "url": "url2"}]')

You can query the results using the methods described here: https://www.postgresql.org/docs/9.4/functions-json.html

You can add the JSON as an array object with this query:

INSERT INTO <YOUR TABLE>(projects) VALUES (array['[{"project": "project1", "url":"url1"},{"project": "project2", "url": "url2"}]'::JSONB])
Sign up to request clarification or add additional context in comments.

6 Comments

No, the type of column is jsonb[ ]. The column should hold array of jsonb objects
sorry did not ready properly. I added the insert array as second query. But I can tell you working with json array objects is gonna be pretty painful...
What should be the better approach then. I am a very novice to Postgresql
You should normalize your table properly. Take the effort and unpackage your json in javascript and insert all values in proper tables and columns. e.g make a project table with an id, name, url etc... Especially if you want to query the data in the json often. The JSON approach seems fast at first but it's gonna get pretty annoying because u have to write complex querys for the simplest things.
So you want to say that I make another project table having application as foreign key and then perform a join query on both tables?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.