Parse JSON in MySQL

前端 未结 3 1647
滥情空心
滥情空心 2020-12-16 08:17

I need help on how to parse JSON data in MySQL.

I can parse a column named config containing data such as:

{\"encounterId\":\"f45bf821-98e1-4496-82ef         


        
相关标签:
3条回答
  • 2020-12-16 08:45

    Here's a solution in MySQL 5.7 syntax:

    select be.config->'$.encounterId' AS eid
    , be.config->'$.providerId' AS gender
    , be.config->'$.patientId' AS pid
    , be.config->'$.formId' AS formid
    from bencounter be \G
    

    Output:

    *************************** 1. row ***************************
       eid: "f45bf821-98e1-4496-82ef-047971e168cb"
    gender: "38001853-d2e1-4361-9fff-cfca1aedf406"
       pid: "f4d04edb-652f-427c-ac25-6fecbda2a0aa"
    formid: "ETAT"
    

    Remember that field keys in JSON are case-sensitive. For example, 'formId' is not the same as 'formid'.

    0 讨论(0)
  • 2020-12-16 08:47

    Either you could use a cumbersome MySQL UDF for parsing JSON for MySQL like for example https://github.com/ChrisCinelli/mysql_json

    ...but a better way would be to pull out the JSON and parse it in your app, and perhaps convert the data to a more suited schema for your intentions.

    0 讨论(0)
  • 2020-12-16 08:55

    It appears you are using https://common-schema.googlecode.com/svn/trunk/common_schema/doc/html/get_option.html. It specifies that subdictionaries are not supported, which I think is your problem here.

    Mysql is not a great tool for parsing JSON.

    I think there are some efforts for future versions like 5.7 to start including some support for JSON (see http://blog.ulf-wendel.de/2014/mysql-5-7-http-plugin-mysql/).

    If you are on an earlier version now you might try using UDFs like http://www.slideshare.net/mobile/SvetaSmirnova/mysql-json-functions

    HTH

    0 讨论(0)
提交回复
热议问题