简体   繁体   English

使用JSON_EXTRACT时遇到路径问题

[英]Struggling with path when using JSON_EXTRACT

I have meta data that is stored in mysql with geostamps. 我有使用geostamps存储在mysql中的元数据。 I want to extract the lat and long for a specific set of data my for the life of me cant figure out the syntax to specify the level down to the value 我想提取特定数据集的经度和纬度,而在我的生命中无法弄清楚语法以将级别指定为该值

I need the values seperate so i can use it to check if that location matches witht he location on record and if not distance away. 我需要单独的值,以便我可以使用它来检查该位置是否与记录中的位置相匹配,以及是否相距不远。 The gps calculation part is simple. gps计算部分很简单。

The string of data I am interested looks like this: 我感兴趣的数据字符串如下所示:

"outcome_rating":{"value":"continue_support","timestamp":"2019-05-29 16:11:07", "geostamp":"lat=-29.787506666666665, long=31.022944999999996, alt=64.0, accuracy=8.2"} “ outcome_rating”:{“ value”:“ continue_support”,“ timestamp”:“ 2019-05-29 16:11:07”,“ geostamp”:“ lat = -29.787506666666665665,long = 31.022944999999996,alt = 64.0,precision = 8.2“}

The table is called "monitorings" The field is called "devicemagic_metadata" 该表称为“监控”。该字段称为“ devicemagic_metadata”

My attempt 我的尝试

SELECT
    select JSON_EXTRACT(devicemagic_metadata, "$.outcome_rating"."$.geostamp"."$.lat")
FROM monitorings;

Any help or direction would be apreciated 任何帮助或指示将不胜感激

Your current JSON content is invalid, because it does contains an outer key which does not appear inside curly braces. 您当前的JSON内容无效,因为它确实包含一个不会出现在花括号内的外键。 Have a look below at what the correct format should be. 请在下面查看正确的格式。 This solution assumes that you are using MySQL 8+, which has a regex replacement capability. 该解决方案假定您使用的是具有正则表达式替换功能的MySQL 8+。 If not, then you would need some really ugly string function calls to isolate the latitude and longitude from the JSON value. 如果不是,那么您将需要一些非常丑陋的字符串函数调用来将纬度和经度与JSON值隔离。

WITH monitorings AS (
    SELECT '{"outcome_rating":{"value":"continue_support","timestamp":"2019-05-29 16:11:07", "geostamp":"lat=-29.787506666666665, long=31.022944999999996, alt=64.0, accuracy=8.2"}}' AS devicemagic_metadata
)

SELECT
    REGEXP_REPLACE(JSON_EXTRACT(devicemagic_metadata, "$.outcome_rating.geostamp"),
        '^"lat=([^,]+),.*$', '$1') AS lat,
    REGEXP_REPLACE(JSON_EXTRACT(devicemagic_metadata, "$.outcome_rating.geostamp"),
        '^.*long=([^,]+),.*$', '$1') AS lng
FROM monitorings;

在此处输入图片说明

Demo 演示版

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM