How to combine JSON_LENGTH and JSON_EXTRACT in MySQL? How to combine JSON_LENGTH and JSON_EXTRACT in MySQL? json json

How to combine JSON_LENGTH and JSON_EXTRACT in MySQL?


If you use MySQL 8.0, you can extract the second last json item with:

JSON_EXTRACT(JsonData, '$[last-1]');

As documented here: 11.6 The Json Data Type.


@len here is a table. You can't automap it to each update row.

You need to inline the length, e.g.

UPDATE MyTable SET ExtractedData = JSON_EXTRACT(JsonData, CONCAT('$[',JSON_LENGTH(JsonData)-1,']'));

Check the fiddle at https://www.db-fiddle.com/f/6PC2CaBxwDdfpruECqAjGR/1