1

For example:

SET @key = '["a","b"]';
SELECT JSON_SEARCH(@key, 'one', 'b');

...will return the path:

"$[1]"

Insert this as the path in JSON_EXTRACT like:

SET @value = '["1","2"]';
SELECT JSON_EXTRACT(@value, "$[1]");

...this will return the value:

"2"

But if I write following:

SET @key = '["a","b"]';
SET @value = '["1","2"]';
SET @path = (SELECT JSON_SEARCH(@key, 'one', 'b'));
SELECT JSON_EXTRACT(@value, @path);

...this will drop an error:

SQL Fehler (3143): Invalid JSON path expression. The error is around character position 1 in '"$[1]"'.

Trimming the double quotes works, but I don't like this solution:

SELECT JSON_EXTRACT(@value, TRIM(BOTH '"' FROM @path));

Is there an other way or am I missing something?

1 Answer 1

4

JSON_PATH returns a JSON object (a JSON string) that needs to be unquoted when used as string:

SELECT JSON_EXTRACT(@value, JSON_UNQUOTE(@path));

"2"

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.