0

We have a database table in MySQL8 (and MariaDB in some deployments) with two columns which each store a JSON array, like

Col A Col B
['a', 'b', 'c'] [10, 20, 30]

Ultimately, we need to create a JSON object from those two arrays that uses Col A as keys and Col B as values, like so

{
    a: 10,
    b: 20,
    c: 30
}

I thought I could use the MySQL function JSON_OBJECT for that, but that requires me to interleave the two columns into one list, which I also struggle with. I guess you could also make use of the JSON_TABLE function in a smart way, but I didn't come up with a solution yet.

I thought this would be a simple task, but it isn't, at least not to me. Can someone with better knowledge of MySQL guide me?

2
  • It is always best for you to show us your workings! Your best attempt. The reason being we can then at least get an idea of how you wanted to approach the problem, and that you have made some attempt at solving the problem rather than just asking someone else to do it for you. Although you should already know this Commented Jan 2, 2024 at 14:43
  • Thanks for your reply (and I assume you linked the related question too). I didn't come up with any tangible idea and was stuck with things floating in my head, probably because I had been at work for too long already. For some reason I didn't find the related question on my own, seems like the solution that I had imagined. Thanks for the link Commented Jan 2, 2024 at 16:39

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.