1

I am testing this example.

JSON_TABLE – The Best of Both Worlds

I understand that t1 is the table and people the array but where do I put the name of the column?

INSERT INTO t1 VALUES (
        '{ "people": [
            { "name":"John Smith",  "address":"780 Mission St, San Francisco, CA 94103"}, 
            { "name":"Sally Brown",  "address":"75 37th Ave S, St Cloud, MN 94103"}, 
            { "name":"John Johnson",  "address":"1262 Roosevelt Trail, Raymond, ME 04071"}
         ] }'
    );

They call the column json_col but they only use it in a select query like this.

SELECT people.* 
FROM t1, 
     JSON_TABLE(json_col, '$.people[*]' COLUMNS (
                name VARCHAR(40)  PATH '$.name',
                address VARCHAR(100) PATH '$.address')
     ) people;
3
  • 2
    json_col is the name of the column ( CREATE TABLE t1(json_col JSON); ). For insertion, you can also use INSERT INTO t1(json_col) VALUES (...) Commented Jan 23, 2019 at 12:51
  • Thanks Pavel, that works. Commented Jan 23, 2019 at 12:54
  • I've also added this comment as answer, co it will be more visible :) Commented Jan 23, 2019 at 13:42

1 Answer 1

1

json_col is the name of the column ( CREATE TABLE t1(json_col JSON); ).

For insertion, you can also use INSERT INTO t1(json_col) VALUES (...)

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

2 Comments

How do I update the same array after the new value is inserted? So I can use a where clause on for example an id column.
Exactly, json_col (you can name the column as you want) is just a regular column with data type JSON.

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.