Home > 语言编程 > [Python]PostgreSQL字典/JSON类型递归自展开

[Python]PostgreSQL字典/JSON类型递归自展开

PostgreSql 习惯上会将特殊数据类型的各个节点按字典/JSON类型存储
程序中需要获得完整的数据信息的时候,需要对这个节点进行自展开。

以下使用global id方式进行展开,一般适用于SQL+NoSQL结合的系统使用

import sys, os
import numpy as np
 
def get_object_by_gid(id):
	for dict in data["json"]:
		if dict["gid"] == id:
	return dict.copy()
 
def self_exact_node(key):
	dict = get_object_by_gid(key)
	for k,v in dict.items():
		if k == "sub_item" :
			item_arr = []
			for id in v["gids"]:
				item_arr.append(self_exact_node(id))
			v["item_arr"] = item_arr.copy()
	return dict
 
def demo():
	data_exact = data.copy()
	for d in data_exact["json"]:
		d = self_exact_node(d["gid"])
 
def main():
	demo()
 
if __name__ == '__main__’:
	sys.exit(main())

其他玩法

# Global ID方式
"gid": "大分类2:中分类2:子分类2",
# 复合ID
"id":{"l1_cat":"大分类1","l2_cat":"中分类2","l3_cat":"子分类2"}
# 链表式
"chain":{"next_gid":"","pre_gid":"","head_gid":"","tail_gid":""}
# 二叉树式
"btree":{"next_sibling":"","child":”"}

测试用数据

data = {
    "json": [{
            "gid": "大分类1",
            "sub_item": {
                "gids": ["大分类1:中分类1", "大分类1:中分类2"]
            },
            "values": {
                "k1": "1000"
            }
        },
        {
            "gid": "大分类2",
            "sub_item": {
                "gids": ["大分类2:中分类1", "大分类2:中分类2"]
            },
            "values": {
                "k1": "2000"
            }
        },
 
        {
            "gid": "大分类1:中分类1",
            "sub_item": {
                "gids": ["大分类1:中分类1:子分类1", "大分类1:中分类1:子分类2"]
            },
            "values": {
                "k1": "1100"
            }
        },
        {
            "gid": "大分类1:中分类2",
            "sub_item": {
                "gids": ["大分类1:中分类1:子分类1", "大分类1:中分类1:子分类2"]
            },
            "values": {
                "k1": "1200"
            }
        },
 
        {
            "gid": "大分类2:中分类1",
            "sub_item": {
                "gids": ["大分类2:中分类1:子分类1", "大分类2:中分类1:子分类2"]
            },
            "values": {
                "k1": "2100"
            }
        },
        {
            "gid": "大分类2:中分类2",
            "sub_item": {
                "gids": ["大分类2:中分类2:子分类1", "大分类2:中分类2:子分类2"]
            },
            "values": {
                "k1": "2200"
            }
        },
 
        {
            "gid": "大分类1:中分类1:子分类1",
            "sub_item": {
                "gids": []
            },
            "values": {
                "k1": "1110"
            }
        },
        {
            "gid": "大分类1:中分类1:子分类2",
            "sub_item": {
                "gids": []
            },
            "values": {
                "k1": "1120"
            }
        },
        {
            "gid": "大分类1:中分类2:子分类1",
            "sub_item": {
                "gids": []
            },
            "values": {
                "k1": "1210"
            }
        },
        {
            "gid": "大分类1:中分类2:子分类2",
            "sub_item": {
                "gids": []
            },
            "values": {
                "k1": "1220"
            }
        },
 
        {
            "gid": "大分类2:中分类1:子分类1",
            "sub_item": {
                "gids": []
            },
            "values": {
                "k1": "2110"
            }
        },
        {
            "gid": "大分类2:中分类1:子分类2",
            "sub_item": {
                "gids": []
            },
            "values": {
                "k1": "2120"
            }
        },
        {
            "gid": "大分类2:中分类2:子分类1",
            "sub_item": {
                "gids": []
            },
            "values": {
                "k1": "2210"
            }
        },
        {
            "gid": "大分类2:中分类2:子分类2",
            "id":{"l1_cat":"大分类1","l2_cat":"","l3_cat":""}
            "sub_item": {
                "gids": []
            },
            "values": {
                "k1": "2220"
            }
        },
 
{
            "gid": "大分类2:中分类2:子分类2",
            /*复合ID*/
            "id":{"l1_cat":"大分类1","l2_cat":"中分类2","l3_cat":"子分类2"}
            /*链表式*/
            "chain":{"next_gid":"","pre_gid":"","head_gid":"","tail_gid":""}
            /*二叉树式*/
            "btree":{"next_sibling":"","child":""}
            "sub_item": {
                "gids": []
            },
            "values": {
                "k1": "2220"
            }
        }
    ]
}
Categories: 语言编程 Tags: , , ,
  1. No comments yet.
  1. No trackbacks yet.
You must be logged in to post a comment.