1

I have meta data that is stored in mysql with geostamps. 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.

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"}

The table is called "monitorings" The field is called "devicemagic_metadata"

My attempt

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

Any help or direction would be apreciated

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360

1 Answers1

0

Your current JSON content is invalid, because it does contains an outer key which does not appear inside curly braces. 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. If not, then you would need some really ugly string function calls to isolate the latitude and longitude from the JSON value.

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;

enter image description here

Demo

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360