2

I am trying to extract json from database, but I am guessing there is malformed json there, and some of them is not valid json, or not exist at all, so I try to do something like this:

SELECT JSON_EXTRACT(json_column, '$.payload.from') as was,
JSON_EXTRACT(json_column, '$.payload.to') as now
FROM orders
WHERE JSON_EXTRACT(json_column, '$.action') = 'change'
AND JSON_EXTRACT(json_column, '$.payload.from') IS NOT NULL
AND JSON_EXTRACT(json_column, '$.payload.to') IS NOT NULL
AND JSON_EXTRACT(json_column, '$.action') IS NOT NULL
AND JSON_VALID(json_column) = 1;

but I am still getting error: "Invalid JSON text in argument 1 to function json_extract: "Invalid value." at position 0."

any thoughts on how to skip this error for malformed JSON row?

1
  • SELECT json_column FROM orders should show the JSON that is stored in the column json_column. Then you can check if the JSON is valid, and correct it (or delete it... ) Commented Jul 22, 2022 at 11:21

1 Answer 1

11

To start with, you should probably try to get a picture of what the invalid data is so that it's not the product of a bug in the logic inserting new data.

To see what's "broken", try;

SELECT json_column FROM order WHERE NOT JSON_VALID(json_column)

If you're satisfied that the invalid data is not actually of importance, you can use a CTE to only parse fields with valid values, that will make sure the validity is checked before the data is parsed;

WITH orders AS (
  SELECT json_column FROM orders WHERE JSON_VALID(json_column)
)
SELECT JSON_EXTRACT(json_column, '$.payload.from') as was,
       JSON_EXTRACT(json_column, '$.payload.to') as now
  FROM orders
 WHERE JSON_EXTRACT(json_column, '$.action') = 'change'
   AND JSON_EXTRACT(json_column, '$.payload.from') IS NOT NULL
   AND JSON_EXTRACT(json_column, '$.payload.to') IS NOT NULL
   AND JSON_EXTRACT(json_column, '$.action') IS NOT NULL

A very simple demo of the concept as db-fiddle

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.