0

Looking for your experience in converting a complicated nested MYSQL query to a JSON file. Below is the code.

SELECT post_id
     , name
     , Email
     , CustomerId
     , DeliveryDate
     , DeliveryTime
     , DeliveryType
     , Zip
     , OrderNote
     , PaymentTotal
     , OrderStatus
  FROM ( SELECT t1.post_id
              , t2.name
              , MAX(CASE WHEN meta_key = 'value' THEN meta_value ELSE NULL END) as Email
              , MAX(CASE WHEN meta_key = 'value' THEN meta_value ELSE NULL END) as CustomerId
              , MAX(CASE WHEN meta_key = 'value' THEN meta_value ELSE NULL END) as DeliveryDate
              , MAX(CASE WHEN meta_key = 'value' THEN meta_value ELSE NULL END) as DeliveryTime
              , MAX(CASE WHEN meta_key = 'value' THEN meta_value ELSE NULL END) as DeliveryType
              , MAX(CASE WHEN meta_key = 'value' THEN meta_value ELSE NULL END) as Zip
              , MAX(CASE WHEN meta_key = 'value' THEN meta_value ELSE NULL END) as OrderNote
              , MAX(CASE WHEN meta_key = 'value' THEN meta_value ELSE NULL END) as PaymentTotal
              , MAX(CASE WHEN meta_key = 'value' THEN meta_value ELSE NULL END) as OrderStatus
           FROM table_A t1
         INNER 
           JOIN table_B t2 
             ON FIND_IN_SET(t1.post_id, t2.payment_ids)  
         GROUP 
             BY t1.post_id
              , t2.name  
       ) AS derived_table
 WHERE OrderStatus RLIKE '%trans%|ready'
   AND DeliveryDate >= CURRENT_DATE - INTERVAL 7 DAY
   AND DeliveryType = 'pickup'

Since this has to start with SELECT is there a way to convert this to JSON properly and what would it look like? As I understand it, JSON doesnt like when you start with the SELECT

4
  • What version of MySQL are you using? Commented Aug 19, 2020 at 2:24
  • I presume all the 'value' strings are actually different and represent the appropriate key for the value you are trying to extract? Commented Aug 19, 2020 at 2:24
  • corerct. 'value' is a placeholder so my answer isnt so specific. I am running 5.7.3 Commented Aug 19, 2020 at 3:01
  • Thanks I would love to but dont have enough Karma to vote. It says, thanks your response has been recorded but doesnt count Commented Aug 20, 2020 at 19:53

1 Answer 1

1

You can use JSON_OBJECT to create a JSON object out of each row that your query produces. These objects then need to be gathered into an array. Since you're not using MySQL 8+, you will need to simulate the JSON_ARRAYAGG by using GROUP_CONCAT instead. For example (I've left out parts of your query for simplicity):

SELECT CONCAT('[', GROUP_CONCAT(obj), ']') AS JSON
FROM (
    SELECT JSON_OBJECT('post_id', post_id
                     , 'name', name
                     , 'Email', Email
                     , 'CustomerId', CustomerId
                       -- ...
                      ) AS obj
    FROM (SELECT t1.post_id
               , t2.name
               , MAX(CASE WHEN meta_key = 'value' THEN meta_value ELSE NULL END) as Email
               , MAX(CASE WHEN meta_key = 'value' THEN meta_value ELSE NULL END) as CustomerId
               -- ...
          FROM table_A t1
          INNER JOIN table_B t2 ON FIND_IN_SET(t1.post_id, t2.payment_ids)  
          GROUP BY t1.post_id, t2.name  
    ) AS derived_table
    WHERE OrderStatus RLIKE '%trans%|ready'
      AND DeliveryDate >= CURRENT_DATE - INTERVAL 7 DAY
      AND DeliveryType = 'pickup'
) j

Small demo on dbfiddle

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. Is there a major advantage here to moving to MYSQL 8?
@Tim there are many advantages, especially with the use of CTEs and window functions, although for this query it wouldn't make that much difference, you'd just change the first line to SELECT JSON_ARRAYAGG(obj)

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.