1

for first stage i suggest that json not nested - plain pairs key-value packed from post request by php script and passed as single json-type parameter to mysql stored procedure - i need get table where two columns - key and corresponding value

set @j:='{"customer_name":"Ворпенсий", "customer_bd":"1952-10-07"}';
select *, json_unquote(json_extract(@j, concat('$.', `name`))) as `value`
from json_table(json_keys(@j), 
              "$[*]" columns(name varchar(100) path "$")) as jt;

code work as expected but how can i join this 2-step solution to 1-step?

3
  • Some sample data would help answer the question.
    – Nick
    Commented May 11, 2022 at 8:01
  • updated code with json-variable Commented May 11, 2022 at 13:00
  • updated code with json_unquote Commented May 12, 2022 at 5:13

1 Answer 1

2

What you are doing is probably close to optimal. You could also achieve the same result by joining two JSON_TABLEs, one of keys and the other of values. For example:

SELECT name, value
FROM JSON_TABLE(JSON_KEYS(@j),
    "$[*]" COLUMNS (
          rn FOR ORDINALITY,
          name VARCHAR(100) PATH '$'
          )
    ) n
JOIN JSON_TABLE(@j, 
    "$.*" COLUMNS (
          rn FOR ORDINALITY,
          value VARCHAR(100) PATH '$'
          )
    ) v ON n.rn = v.rn

Output (for your sample data):

name            value
customer_bd     1952-10-07
customer_name   Ворпенсий

Demo on dbfiddle

2
  • thank you very much for support & link to dbfiddle Commented May 12, 2022 at 5:13
  • @СлаваСедов no worries - I hope you find it useful.
    – Nick
    Commented May 12, 2022 at 5:28

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.