简体   繁体   English

sqlite中使用json_extract从父子对象中拉取数据

[英]Using json_extract in sqlite to pull data from parent and child objects

I'm starting to explore the JSON1 library for sqlite and have been so far successful in the basic queries I've created.我开始探索 sqlite 的 JSON1 库,到目前为止,我在创建的基本查询中取得了成功。 I'm now looking to create a more complicated query that pulls data from multiple levels.我现在正在寻找创建一个更复杂的查询,从多个级别提取数据。

Here's the example JSON object I'm starting with (and most of the data is very similar).这是我开始使用的示例 JSON object(大部分数据非常相似)。

{
  "height": 140.0,
  "id": "cp",
  "label": {
    "bind": "cp_label"
  },
  "type": "color_picker",
  "user_data": {
    "my_property": 2
  },
  "uuid": "948cb959-74df-4af8-9e9c-c3cb53ac9915",
  "value": {
    "bind": "cp_color"
  },
  "width": 200.0
}

This json object is buried about seven levels deep in a json structure and I pulled it from the larger json construct using an sql statement like this:这个 json object 被埋在 json 结构中大约七层深处,我使用 sql 语句从更大的 json 结构中提取它,如下所示:

SELECT value FROM forms, json_tree(forms.formJSON, '$.root') 
WHERE type = 'object'
    AND json_extract(value, '$.id') = @sControlID

// In this example, @sControlID is a variable that represents the `id` value we're looking for, which is 'cp'

But what I really need to pull from this object are the following:但我真正需要从这个 object 中提取的是以下内容:

  • the value from key type ("color_picker" in this example)来自键type的值(本例中为“color_picker”)
  • the values from keys bind ("cp_color" and "cp_label" in this example)来自键bind的值(本例中为“cp_color”和“cp_label”)
  • the keys value and label (which have values of {"bind":"<string>"} in this example) valuelabel (在此示例中其值为{"bind":"<string>"}

For that last item, the key name ( value and label in this case) can be any number of keywords, but no matter the keyword, the value will be an object of the form {"bind":"<some_string>"} .对于最后一项,键名(在本例中为valuelabel )可以是任意数量的关键字,但无论关键字如何,值都将是{"bind":"<some_string>"}形式的 object。 Also, there could be multiple keys that have a bind object associated with them, and I'd need to return all of them.此外,可能有多个键具有与之关联的bind object,我需要返回所有这些键。 For the first two items, the keywords will always be type and bind .对于前两项,关键字始终是typebind

With the json example above, I'd ideally like to retrieve two rows:对于上面的 json 示例,理想情况下我想检索两行:

type          key     value
color_picker  value   cp_color
color_picker  label   cp_label

When I use json_extract methods, I end up retrieving the object {"bind":"cp_color"} from the json_tree table, but I also need to retrieve the data from the parent object. I feel like I need to do some kind of union, but my attempts have so far been unsuccessful.当我使用 json_extract 方法时,我最终从 json_tree 表中检索 object {"bind":"cp_color"} ,但我还需要从父 object 中检索数据。我觉得我需要做某种联合,但我的尝试到目前为止都没有成功。 Any ideas here?这里有什么想法吗?

Note: if the {"bind":"<string>"} object doesn't exist as a child of the parent object, I don't want any rows returned.注意:如果{"bind":"<string>"} object 不作为父 object 的子项存在,我不希望返回任何行。

Well, I was on the right track and eventually figured out it.好吧,我走在正确的轨道上并最终弄明白了。 I created a separate query for each of the items I was looking for, then INNER JOIN ed all the json_tree tables from each of the queries to have all the required fields available.我为我正在寻找的每个项目创建了一个单独的查询,然后INNER JOIN从每个查询编辑所有json_tree表,以提供所有必需的字段。 Then I json_extract ed the required data from each of the json fields I needed data from.然后我json_extract从我需要数据的每个 json 字段中编辑了所需的数据。 In the end, it gave me exactly what I was looking for, though I'm sure it could be written more efficiently.最后,它给了我想要的东西,尽管我确信它可以写得更有效率。

For anyone interested, this is what hte final query ended up looking like:对于任何感兴趣的人,这就是最终查询最终的样子:

SELECT IFNULL(json_extract(parent.value, '$.type'), '_window_'), child.key, json_extract(child.value, '$.bind') FROM (SELECT json_tree.* FROM nui_forms, json_tree(nui_forms.formJSON, '$') WHERE type = 'object' AND json_extract(nui_forms.formJSON, '$.id') = @sWindowID) parent INNER JOIN (SELECT json_tree.* FROM nui_forms, json_tree(nui_forms.formJSON, '$') WHERE type = 'object' AND json_extract(value, '$.bind') != 'NULL' AND json_extract(nui_forms.formJSON, '$.id') = @sWindowID) child ON child.parent = parent.id;

If you have any tips on reducing its complexity, feel free to comment!如果您有任何降低其复杂性的技巧,请随时发表评论!

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

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