0

I have two tables, and I want to use a value from primary table (here: person.ref) to extract some data from a json field in a secondary table (here: person_details):

create table person ( 
    id int(8),
    ref varchar(20),
    primary key (id)
);

create table person_details (
    id int(8),
    details json,
    primary key (id)
);

SELECT JSON_EXTRACT(details, CONCAT('$.', p.ref))
FROM person p
JOIN person_details d using (id);

The person_details data is like:

{
  "ref1": {
    ...
  }
}

But that does not matter, because the sql statement itself seems to be invalid:

Result:

#3143 - Invalid JSON path expression. The error is around character position 12.

Why?

1
  • Provide sample data as INSERT INTO and desired output for it. Commented Nov 9, 2022 at 10:42

2 Answers 2

2

Your code should work on MySQL >= 5.7. Check this dbfiddle:

https://dbfiddle.uk/esEJ9uHd

CREATE TABLE `person` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `ref` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `person_details` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `details` json,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO `person` (`id`, `ref`) VALUES (1, 'ref1');
INSERT INTO `person` (`id`, `ref`) VALUES (2, 'ref1');
INSERT INTO `person_details` (`id`, `details`) VALUES (1, '{"ref1":  {"name": "Barney"}}');
INSERT INTO `person_details` (`id`, `details`) VALUES (2, '{"ref1":  {"name": "Willma"}}');

SELECT JSON_EXTRACT(details, CONCAT('$.', p.ref))
FROM person p
JOIN person_details d using (id);
Sign up to request clarification or add additional context in comments.

6 Comments

In MySQL 5.7 there exists JSON datatype - what is the reason for LONGTEXT using? And you do not need to quote doublequote chars in the value.
I created the table in NAVICAT for testing and this is how he created the table definition DDL when I chose the field type JSON. On localhost I have 10.4.24-MariaDB. JSON is converted in longtext -> mariadb.com/kb/en/json-data-type
But then you copy this to fiddle - you'd fix this. Additionally - you create fiddle using version 8.0. On version 5.7, your CHECK constraint will be cut out of the code - this version parses but ignores it.
On localhost I have 10.4.24-MariaDB. Yes, MariaDB have not implemented JSON datatype yet.
You're correct: on empty databases the sql statement works without problems (of course without results). So it somehow has to do with the data inside, not the statement itself.
|
0

While the answer above is correct, it did not solve my issue, but I had to add doublequotes around p.ref, so that the concatenation results in CONCAT($."p.ref"):

SELECT JSON_EXTRACT(details, CONCAT('$."', p.ref, '"'))
...

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.