5

While searching for a way to insert array into single DB columns, I found an article about inserting JSON strings. However it wasn't explained how. I tried to search and find the way with no success.

I have the following table:

Name Type
id int AI
name String
address JSON(longtext)

What I want to do is insert a JSON array in the address column, like:

id name address
1 User name [{street: "street address", city: "Berlin"}]

I thought about inserting the JSON as String but I'm not sure if this a good idea. Any suggestions?

2
  • 1
    No don't convert the json to string. You already have json datatype in MySQL from 5.7 version. Insert the column directly. It accepts. Commented Dec 12, 2020 at 19:14
  • The JSON you show is not valid JSON. You need to delimit keys with double-quotes. Commented Dec 12, 2020 at 19:17

6 Answers 6

17

You can pass your data as a string, as long as it is valid JSON; MySQL will happily convert it for you under the hood.

insert into mytable (id, name, address)
values (
    1,
    'User name',
    '[{"street": "street address", "city": "Berlin"}]'
);

An alternative is to use JSON builder functions:

insert into mytable (id, name, address)
values (
    1,
    'User name',
    json_array(json_object('street', 'street address', 'city', 'Berlin'))
);
Sign up to request clarification or add additional context in comments.

Comments

3

This works in my case. In summary use ARRAY[CAST('{"key": "val"}' as json)].

Example:

insert into <table_name> (id, name, address)
values ('1', 
        'User name', 
         array[cast('{"street": "street address", "city": "Berlin"}' as json)])

Comments

1
CREATE TABLE people (id INT, name VARCHAR(255), address JSON);

INSERT INTO people (id, name, address)
VALUES (1, 'User name', '[{"street": "street address", "city": "Berlin"}]');

SELECT *
FROM people;
id name address
1 User name [{"city": "Berlin", "street": "street address"}]

db<>fiddle here

1 Comment

Thank you for your replay. much appreciate it
1

@GMB's alternative answer looks correct but I thought I would add another answer to show how I implemented this in a format that I prefer:

INSERT INTO <table name>  
SET field1 = '123',
    field2 = json_array(‘456’);

I like listing my field names next to their values so I don't have to mentally map the values to the variables.

Also, I didn't need the json_object since I was not entering a key:value pair - I was just entering a list of values.

1 Comment

Tbh. This is the answer I was looking for :). The other answer is also good and working.
0

Why converting to String if you already have a datatype JSON in MySQL

INSERT INTO <Table_Name> VALUES ('{street: "street address", city: "Berlin"}');

This insert statement will directly inserts your array. There is no need to convert.

See this stackoverflow answer

1 Comment

Thank you man. i appreciate your replay and expain
0

INSERT INTO TABLE_NAME (STREET, CITY) VALUES (?,?),[STREET,CITY]

For best practice you can utilize the above mentioned format for handling dynamic data

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.