将MySQL中的JSON字符串转换为标头和值

时间:2019-03-12 15:54:10

标签: mysql json python-3.x data-conversion

我有一个带有JSON长文本的表:

id | data
01 | {JSON string - ~ 10 headers, including nested headers}
02 | {JSON string - ~ 9 headers, including nested headers}

我正在尝试在其自己的标头中包含JSON字符串并具有其自己的值,同时确保它带有其ID。

我已经完成了与python的连接,因为它本地支持JSON。查询数据库后,结果如下:

(01,'{“ JSON变量”}')

我认为可以归结为:

  1. 我如何能够动态地从JSON变量中提取标头?

  2. 我可以将其映射回ID吗?

  3. 如何将其插入回SQL数据库?

任何帮助或指导都会有用。我知道这是一项艰巨的任务。

JSON字符串示例

{"flexible_spec": [
    {"behaviors": [
        {"id": "6015559470583", "name": "Lives abroad"
        }, 
        {"id": "6025753961783", "name": "Family of those who live abroad"
        }, 
        {"id": "6025978286383", "name": "Close friends of those who live abroad"}
        ]
        }
    ], 
    "targeting_optimization": "none", 
    "publisher_platforms": ["facebook", "instagram", "audience_network", "messenger"], 
    "geo_locations": {"location_types": ["home", "recent"], 
                    "countries": ["GB"]
                    }, 
    "excluded_custom_audiences": [
            {"id": "6129724711690", 
            "name": "Completed Transactions"}
        ], 
    "instagram_positions": ["stream"], 
    "user_device": ["Android_Smartphone", "Android_Tablet"], 
    "age_min": 18, 
    "app_install_state": "not_installed", 
    "device_platforms": ["mobile"], 
    "user_os": ["Android"], 
    "facebook_positions": ["feed", "suggested_video", "instream_video"], 
    "age_max": 65, 
    "messenger_positions": ["messenger_home", "story"], 
    "audience_network_positions": ["classic", "rewarded_video"]
}

如果您需要更多上下文(存在具有多个值的标头,将它们保留在同一行/字段中很好,或者如果它更容易具有相同ID的多行)

到目前为止我做了什么:https://gist.github.com/alvichy/f774fdc6a870257d8bef3ac4b78e01e9

0 个答案:

没有答案