0

How to change JSON text of MySql database to JSON datatype of postgres sql.I am storing JSON data in database table of Mysql and want to convert it into JSON datatype of postgres sql

Mysql create table

CREATE TABLE IF NOT EXISTS `employee` (
  `id` bigint(40) NOT NULL AUTO_INCREMENT,
  `employee_id` bigint(40) NOT NULL,
  `employee_info` text NOT NULL,
  )

In employee_info I am storing {"employee_name":"abc","address":"1","emloyee_weight":"30","age":"100","phone":"9845236775"} in MySql database which is a JSON .

and wanted to convert it into JSON datatype of postgressql

1 Answer 1

1

Question shouldn't left unanswered :-)

It's pretty simple:

Postgresql CREATE TABLE

CREATE TABLE employee (
  id bigserial primary key,
  employee_id bigint NOT NULL,
  employee_info json NOT NULL
);

And then you can some magic like:

select * from employee where employee_info->>'address' like '%Kr%';

or

select * from employee where CAST(employee_info->>'emloyee_weight' AS integer)>30;

Heed on postgres version, it should be 9.3 or greater

http://sqlfiddle.com/#!15/edb87/1

Sign up to request clarification or add additional context in comments.

Comments

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.