简体   繁体   中英

JQ using an ETL, to create an inner array, in a JSON Array

I need to update an inner array, with an ETL;

I want to create a new property in an array's element in the JSON tree

Something like

    def insideETL(keyname; arrayname; cond; result):
      def etl:
        . as $parent
        | .[arrayname][]
        | { parent: $parent, child: .}
        | select(cond) | result;

      map ( . 
            +
            {(keyname): map(etl)}
          )
    ;

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 ;

Data to filter

[
 {
        "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
            }
        ]
    },
]

Desired invocation

.| 
  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
   }
)

Desired output

    [
        {
            // 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)}
 ;

Here's one way to do so:

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") + .[]]

With etl you have a reusable component that allows numerous variations while keeping things simple.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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