如何在Python中基于另一个JSON过滤一个JSON



我试图编写一个Python类,该类接受json请求和json数组,然后根据请求中的数据过滤数组。以下是项目的基本文件夹结构:

my_input/
/request.json
/array.json
my_python/
/class.py

request.json具有数据";位置:1〃;以及";性状:3〃;,并且具有以下形式:

{
"metadata": {
"id": "request",
},
"data": {
"place": [
"1"
],
"trait": [
"3"
]
}
}

array.json有两个子数组,locationArraymeasurementArray;在locationArray中,我们看到位置1与图2和图3相关联:

{  "data":{
"measurementArray": {
"headers": ["plot","trait","value"],
"data": [
[1, 3, 2.7],
[2, 2, 1.8],
[3, 3, 3.6]
], 
"locationArray": {
"headers": ["place","plot"],
"data": [
[1,2],
[3,4],
[1,3]
], 
}}}

然后我们过滤图2和图3;性状:3〃;。在这个例子中,它将只返回测量数组中的一行,因为只有位于图2或图3的一行具有特征3:

[3,3,3.6]

如何编写一个类来解析请求json,然后在第二个json中过滤数组?目前,我只有一节空白课。

非常感谢您考虑这个问题!

我不确定它是否适用

class my_input:
def __init__(self,my_request, my_array):
self.my_request = my_request
self.my_array = my_array
def get_places(self):
return self.my_request['data']['place']
def get_traits(self):
return self.my_request['data']['trait']
def get_associated_plots(self,places):
associated_plots = []
place_st = set(places)
for current_data in self.my_array['data']['measurementArray']['locationArray']['data']:
if str(current_data[0]) in place_st:
associated_plots.append(current_data[1])
return associated_plots
def get_rows(self,associated_plots,traits):
triats_st = set(traits)
associated_plots_st = set(associated_plots)
for current_data in self.my_array['data']['measurementArray']['data']:
rows = []
if current_data[0] in associated_plots_st and str(current_data[1]) in triats_st:
rows.append(current_data)
return rows

最新更新