JQ使用ETL在JSON数组中创建内部数组
我需要使用ETL更新内部数组;
I need to update an inner array, with an ETL;
我想在JSON树的数组元素中创建一个新属性
I want to create a new property in an array's element in the JSON tree
def insideETL(keyname; arrayname; cond; result):
def etl:
. as $parent
| .[arrayname][]
| { parent: $parent, child: .}
| select(cond) | result;
map ( .
+
{(keyname): map(etl)}
)
;
来自先前的问题
几乎具有所需的结果,但是我确实需要在 Main JSON数组的每个项目中创建多个数组;
From a previous question
Had almost the needed result, but i do have the need to create more than one array, in each item of the Main JSON array;
要过滤的数据
[
{
"storeId": "s2",
"storehouseInfo": {
"id": "025453",
"name": "00211 NW, OR",
"maxPallets": 10
},
"workorder":{
"id": "w2s2",
"startDate": "2019-09-06T10:00:00.000Z",
"vendorId":"v2"
},
"events": [
{
"id": "e4",
"storeId": "s2",
"vendorId": "v1",
"startDate": "2019-09-05T10:00:00.000Z",
"endDate": "2019-09-14T00:00:00.000Z",
"palletsUsed": 5
},
{
"id": "e5",
"storeId": "s2",
"vendorId": "v2",
"startDate": "2019-09-05T00:00:00.000Z",
"endDate": "2019-09-14T00:00:00.000Z",
"palletsUsed": 5
},
{
"id": "e10",
"storeId": "s2",
"vendorId": "v1",
"startDate": "2019-09-06T10:00:00.000Z",
"endDate": "2019-09-14T00:00:00.000Z",
"palletsUsed": 5
},
{
"id": "e11",
"storeId": "s2",
"vendorId": "v2",
"startDate": "2019-09-06T00:00:00.000Z",
"endDate": "2019-09-14T00:00:00.000Z",
"palletsUsed": 5
},
{
"id": "e12",
"storeId": "s2",
"vendorId": "v2",
"startDate": "2019-09-06T10:00:00.000Z",
"endDate": "2019-09-14T00:00:00.000Z",
"palletsUsed": 5
}
]
},
]
所需的调用
.|
insideETL("conflictsInPeriod";
"events";
( (.parent.workorder.startDate | dateDaysAgo(12*7) ) < .child.endDate)
and
(.child.vendorId == .parent.workorder.vendorId);
{
event: .child.id,
wo_sd: .parent.workorder.startDate[:10],
workorder_id: .parent.workorder.id
}
)
所需的输出
[
{
// our newly added array
"conflictsInPeriod":[
{
"event":"e5",
"workorder_sd":"2019-09-06",
"workorder_id":"w2s2"
},
{
"event_id":"e11",
"workorder_sd":"2019-09-06",
"workorder_id":"w2s2"
},
{
"event_id":"e12",
"workorder_sd":"2019-09-06",
"workorder_id":"w2s2"
}
],
// all the other previous information in the Item
"storeId":"s2",
"storehouseInfo":{
"id":"025453",
"name":"00211 NW, OR",
"maxPallets":10
},
"workorder":{
"id":"w2s2",
"startDate":"2019-09-06T10:00:00.000Z",
"vendorId":"v2"
},
"events":[
// ... All the events data
]
}
]
希望很清楚,如果需要任何澄清...请发表评论.
Hope it is clear, If it is needed any clarification... please comment.
我认为,与其束手无策,不如以已经开发和测试的可重用组件为基础:
Rather than tying yourself up in knots, it would I think be better to build on the reusable component that's already been developed and tested:
def etl(keyname; arrayname; cond; result):
def etl:
. as $parent
| .[arrayname][]
| { parent: $parent, child: .}
| select(cond) | result;
{(keyname): map(etl)}
;
这是一种方法:
def add(arrayname):
etl(arrayname;
"events";
( (.parent.workorder.startDate | dateDaysAgo(12*7) ) < .child.endDate)
and
(.child.vendorId == .parent.workorder.vendorId);
{
event: .child.id,
wo_sd: .parent.workorder.startDate[:10],
workorder_id: .parent.workorder.id
}
)
;
[add("conflictsInPeriod") + .[]]
使用etl
,您有一个可重用的组件,该组件可进行多种变化,同时保持简单.
With etl
you have a reusable component that allows numerous variations while keeping things simple.