How to insert Json data into a postgres table using pg-promise - json

how to add json data into POSTGRES table. like if i have a table with id and name. i should add { id : 1, name : 'abc'} into the table in both fields like id -> 1, name -> abc.

Related

Concatenate Postgresql jsonb column with row data

I have a table of data with and id column and a jsonb column with object data:
id (int)
data (jsonb)
I'm querying the data like this
select row_to_json(t)
from (
select id, data from accounts
) t;
which gets me data that looks like this:
{
"id":3,
"data":
{
"emailAddress": "someone#gmail.com",
"mobileNumbers": ["5559991212"]
}
}
I would like to merge the data field into the main record set, I basically want the keys in the data node into the main record:
{
"id":3,
"emailAddress": "someone#gmail.com",
"mobileNumbers": ["5559991212"]
}
You can use
SELECT jsonb_set(data, '{id}', to_jsonb(id))
FROM accounts;
I cannot help remarking that a table with just a primary key and a jsomb column seems like a problematic database design to me.

Problem with copying JSON file as one row in into a table in PostgreSQL

I have a simple JSON document "example_1.JSON:
{
"fruit": "Apple",
"size": "Large",
"color": "Red"
}
I created a temp table "temp_json" to copy the file into it:
CREATE TEMP TABLE temp_json (mydata text);
I copied the JSON file using the following statement:
COPY temp_json from 'C:\Program Files\PostgreSQL\9.5\data\example_1.JSON';
It is ok with the copy part. When I insert the values from the temp table into my database table "jsontable", the insertion happens with no errors, but it inserts the JSON values in several rows inside my database table!!!
My database table is created as follows:
CREATE TABLE public.jsontable (
id bigint NOT NULL DEFAULT nextval('jsontable_id_seq'::regclass),
jsondata jsonb,
CONSTRAINT jsontable_pkey PRIMARY KEY (id)
);
The insert statement from the temp table to the jsontable:
INSERT INTO jsontable(jsondata) SELECT to_jsonb(mydata::text) FROM temp_json;
But when I select rows from jsontable, I don't get the JSON values in a single row!
SELECT * FROM jsontable;
Any suggestions to solve this problem?
You have two options. Control the data from the source file, i.e. pre-format the contents of the file to have them on a single line before copying.
Another option might be to concatenate the lines using string_agg. To maintain the order, I would suggest you to have a default identity column in your temp table.
create sequence seq_temp_json;
CREATE temp TABLE temp_json
(
id INT DEFAULT NEXTVAL('seq_temp_json'::regclass),
mydata TEXT
);
Now, load the temp table and check the order, json order should be in the ascending order of id.
COPY temp_json(mydata) from 'C:\Program Files\PostgreSQL\9.5\data\example_1.JSON';
knayak=# select * from temp_json;
id | mydata
----+-------------------
1 | {
2 | "fruit": "Apple",
3 | "size": "Large",
4 | "color": "Red"
5 | }
(5 rows)
Load the JSON into main table
INSERT INTO jsontable ( jsondata )
SELECT string_agg( mydata ,e'\n' ORDER BY id)::jsonb
FROM temp_json;
The column now contains the complete JSONB.
knayak=# select * from jsontable;
id | jsondata
----+-----------------------------------------------------
6 | {"size": "Large", "color": "Red", "fruit": "Apple"}
(1 row)

insert data as an array in MySql using laravel 5.4

How i can insert multiple id of a table in a single field of another table using laravel 5.4 for exemple :
--------------
field
--------------
2-6-12-7...
--------------
At first, you can make multiple separated ids as a string and then you can insert. Before inserting you have to sure that the field type is varchar or string.
Code Example
$userIds = User::select('id')->get()->toArray();
$joinIds = implode("-", array_column($userIds, "id"));
Now I will Insert into another table which name is Post and has a column name is user_id. The column user_id type will be varchar or string.
$post = new Post();
$post->user_id = $joinIds;
$post->save();

Import to PostgreSQL from partly csv, partly JSON

I'm running PostgreSQL 9.6, and I've got a table consisting of lots of columns.
I've got a csv-file containing the following format:
id, insert_time, JSON-object
The JSON-object has the following format:
{ column_nameY: valueX, column_nameY: valueY, ... }
The column_names in the JSON-object matches the columns in my PostgreSQL-table.
Is there a dynamic way to import such file, so I'll get the id, insert_time, and the remaining column values from the JSON object?
The order of columns in the JSON object might not match the order of the columns in the PostgreSQL table.
I am assuming you know how to get that csv file imported into postgresql and that you know the fields inside that json object field.
first we create a table to store the contents of the csv file. Notice the datatype of that JSON field is jsonb.
create table test11 (id int, insert_time timestamp, json_object jsonb )
Know you import the csv file. but for illustration purposes, i will insert sample data into this Table.
insert into test11 (id, insert_time, json_object) values (1, '2017-11-14'::timestamp, '{ "column_nameX": "3", "column_nameY": "4" }'::jsonb);
insert into test11 (id, insert_time, json_object) values (1, '2017-11-14'::timestamp, '{ "column_nameX": "13", "column_nameY": "14" }'::jsonb);
we now select from that table;
Select id, insert_time, json_object->>'column_nameY' as Column_NameY, json_object->>'column_nameX' as Column_NameX from test11
your results should look like this...
id |insert_time |column_namey |column_namex
1 |11/14/2017 |4 |3
1 |11/14/2017 |14 |13
-HTH

Create a enum table and access it from other table in mysql

I have a table schema as follows:
[enumeration]
Status{
good, bad, high,low
}
Image{
id :string
name: string
quality : Status
}
I have found that enum Table can be created as:
Create Table Status{
status enum ('good','bad','high','low')
};
My question: Do i need to create a column for a table Status?
How can i refer it in other table while creating a table schema?
You don't need a table for status. You declare a status enum column in your image table. Like so:
create table testo (status enum('foo', 'bar', 'baz'));
insert into testo(status) values('foo');