mysql 从JSON字段提取文本

ioekq8ef  于 12个月前  发布在  Mysql
关注(0)|答案(1)|浏览(206)

我的查询看起来像:

select
    id,
    json_extract(flow_settings, '$.email_settings_id') AS email_settings_id
from smsbump.flows
where flows.flow_trigger in ('Synergy/TierExpiryReminder',
        'integrations/swell_tier_earned',
        'integrations/swell_birthday',
        'integrations/swell_points_reminder',
        'integrations/swell_redemption_reminder',
        'integrations/cdp_optin_status_changed',
        'integrations/cdp_redemption_created') and flows.channel in ('email', 'all');

字符串
我的json看起来是这样的:

{
   "platform":"shopify",
   "flow_type":"predefined",
   "cached":false,
   "flow":{
      "trigger":"integrations\/swell_redemption_reminder",
      "triggerOptions":[
         
      ],
      "steps":[
         {
            "id":1,
            "positive":2,
            "negative":false,
            "type":"action",
            "settings":{
               "notify":"customer",
               "ab_testing":{
                  "enabled":false,
                  "message_count":2,
                  "threshold_type":"orders",
                  "threshold_value":5,
                  "messages":[
                     
                  ]
               },
               "message":"{SiteName}: Hey {FirstName}, we haven't seen you in a while! Did you know you have {YotpoPointsBalance} points as part of our rewards program? That's enough for a discount!\nEvery 100 point =$10\n\nShop and redeem now! {SiteUrl}\n\nReply {StopToOpt}",
               "stop_to_opt_out_text":"STOP to opt out",
               "discount_code_settings":false,
               "recommended_product":[
                  
               ],
               "type":"sms",
               "addingMedia":false,
               "mediaPath":"",
               "mediaFile":[
                  
               ],
               "contact_card":false,
               "image":"",
               "contact_card_data":{
                  "name":"",
                  "logo":"",
                  "path":"",
                  "message":"{SiteName}: Save our number to your contact list and never miss a special deal from us. Be the first to know about new product launches and more."
               },
               "event":"integrations\/swell_redemption_reminder",
               "active":true
            }
         },
         {
            "id":2,
            "positive":false,
            "negative":false,
            "type":"action",
            "settings":{
               "discount_code_settings":false,
               "type":"email",
               "personalized_recommended_product":false,
               "active":true,
               "email_settings_id":3023222
            }
         }
      ],
      "name":"Loyalty Redemption Reminder (SMS and Email)",
      "status":1,
      "id":625727,
      "created_user_id":56416521,
      "select_values_to_request":[
         
      ]
   }
}


所以问题是在执行查询后,email_settings_id对于所有记录都是null。
我还尝试在查询中像$.flow.steps.email_settings_id一样构造路径,但仍然返回null。

EDIT我现在看到的是字段实际上是mediumtext而不是json。有没有办法在查询执行过程中转换它?没有权限修改那个表。

ezykj2lf

ezykj2lf1#

你不能访问email_settings_id,因为它不在顶层,它在数组flow_settings->flow.steps中。
您需要将此JSON数组转换为行,然后使用json_extract()或运算符->检索email_settings_id

SELECT id, element->'$.settings.email_settings_id'
FROM flows
CROSS JOIN JSON_TABLE(
         flow_settings->"$.flow.steps",
         "$[*]"
         COLUMNS(
           element JSON PATH "$"
         )
       ) data;

字符串
Demo here

相关问题