简体   繁体   English

如何在 SQL 数据库中组织嵌套的 JSON 对象数据?

[英]How do I organize Nested JSON Objects Data in a SQL Database?

I have a JSON file with some data, like this:我有一个包含一些数据的 JSON 文件,如下所示:

{
    "meta":{
        "server":"awesome.com",
        "user":"awesomeUser",
        "response":"200"
    },
    "data":{
        "tags":["campaign","awesome"],
        "parameters":[{"$ref":"#/components/parameters/campaign"}],
        "responses":{
            "campaign":{
                "name":"My first awesome campaign",
                "description":"This is an amazing campaign",
                "content":{
                    "id":"000001",
                    "publisher":"awesome",
                    "active":true,
                    "launched":354658465,
                    "users":{
                        "countries":{
                            "USA":"753365"
                        }
                    }
                }
            },
            "publisher":{
                "name":"awesome",
                "id":"000099",
                "authorized":true,
                "defaultCurrency":"USD"
            }
        }
    }
}

I need to build a database with this data, but I'm not sure how to organize the tables, specially the nested objects.我需要用这些数据构建一个数据库,但我不确定如何组织表,特别是嵌套对象。 For example, I know one table could be "meta", that would have the server, user and response columns.例如,我知道一个表可能是“元”,它会有服务器、用户和响应列。 then the data table that has the tags, parameters and responses rows, but those are an array, an array of objects and an objetc that has more objects in it.然后是包含标签、参数和响应行的数据表,但这些是一个数组、一个对象数组和一个包含更多对象的 objetc。

How do I manage that in the database?我如何在数据库中管理它?

I'm trying this from scratch because I'm learning, is there a way of making it a little more easier?我正在从头开始尝试这个,因为我正在学习,有没有办法让它更容易一点?

Please kindly let me know if I explained myself correctly and thank you so much in advance.请让我知道我是否正确解释了自己,并在此先感谢您。

It's understandable that you are confused about the best way to store your JSON data.您对存储 JSON 数据的最佳方式感到困惑是可以理解的。

The way to organize nested JSON documents is more or less equivalent to organizing denormalized relational tables.组织嵌套 JSON 文档的方式或多或少等同于组织非规范化的关系表。

You have to start with the queries you need to run.您必须从需要运行的查询开始。 That determines the best way to organize the data.这决定了组织数据的最佳方式。 You store the data in a form that is most convenient for the lookups you will need to do.您可以将数据存储在对您需要执行的查找最方便的形式中。

If you don't know what queries will be needed, or if you have a variety of different types of queries, then the best strategy is to organize data by rules of normalization , and store them in multiple tables.如果您不知道需要什么查询,或者您有多种不同类型的查询,那么最好的策略是按规范化规则组织数据,并将它们存储在多个表中。 Ie do not use JSON at all, use normal rows and columns.即根本不使用 JSON,使用普通的行和列。 This at least reduces redundancy and makes the data support the broadest variety of different queries in the future.这至少减少了冗余,并使数据在未来支持最广泛的不同查询。

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

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