Skip to content
Advertisement

how to extract and zip values from json with SQL, mysql

I struggle to extract the required values from the JSON column in MySQL. Imagine the variable:

and I need to extract and return a new JSON array containing only selected key-value pairs, eg.: only a’s and b’s and if the required key does not exist I want to return null for that key:

thanks

Advertisement

Answer

Here’s a solution using MySQL 8.0’s JSON_TABLE() function:

Based on this result, we can build a new JSON array of objects:

If you are using MySQL 5.x, I would recommend just fetch the whole JSON document to a client application where you can deserialize it into data structures to manipulate in code.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement