dimanche 6 mars 2022

Select random id from list in jq and update value

I have some sample payload that I am going to be receiving, it looks like this:

[
  {
    "Id": "9",
    "Line": [
      {
        "Amount": 100,
        "Description": "Weekly Gardening Service",
        "DetailType": "SalesItemLineDetail",
        "Id": "1",
        "LineNum": 1,
        "SalesItemLineDetail": {
          "ItemAccountRef": {
            "name": "Landscaping Services",
            "value": "45"
          },
          "ItemRef": {
            "name": "Gardening",
            "value": "6"
          },
          "Qty": 4,
          "TaxCodeRef": {
            "value": "TAX"
          },
          "UnitPrice": 25
        }
      },
      {
        "Amount": 100,
        "DetailType": "SubTotalLineDetail",
        "SubTotalLineDetail": {}
      }
    ]
  },
  {
    "Id": "10",
    "Line": [
      {
        "Amount": 140,
        "Description": "Weekly Gardening Service",
        "DetailType": "SalesItemLineDetail",
        "Id": "1",
        "LineNum": 1,
        "SalesItemLineDetail": {
          "ItemAccountRef": {
            "name": "Landscaping Services",
            "value": "45"
          },
          "ItemRef": {
            "name": "Gardening",
            "value": "6"
          },
          "Qty": 4,
          "TaxCodeRef": {
            "value": "NON"
          },
          "UnitPrice": 35
        }
      },
      {
        "Amount": 35,
        "Description": "Pest Control Services",
        "DetailType": "SalesItemLineDetail",
        "Id": "2",
        "LineNum": 2,
        "SalesItemLineDetail": {
          "ItemAccountRef": {
            "name": "Pest Control Services",
            "value": "54"
          },
          "ItemRef": {
            "name": "Pest Control",
            "value": "10"
          },
          "Qty": 1,
          "TaxCodeRef": {
            "value": "NON"
          },
          "UnitPrice": 35
        }
      },
      {
        "Amount": 175,
        "DetailType": "SubTotalLineDetail",
        "SubTotalLineDetail": {}
      }
    ]
  }
]

These I know are valid and I need to cross reference them, by id, in another payload I am receiving. But, the data I am receiving I can't assume to have valid ID's.

So, I want to take all the valid Ids from above, and shove them, randomly, into the sample data I have, that looks like this ($.invoices[].qbId):

[
  {
    "id": "fb2430c5-5970-46b0-9947-aaa0b9f177bb",
    "invoices": [
      {
        "description": "2022-02-03 - 179",
        "dueDate": "2022-02-03T22:51:10.206Z",
        "id": "6f904b18-71c6-4fec-a016-7452f6a6b1dc",
        "invoiceDate": "2022-02-03T22:51:10.347Z",
        "openBalance": 200,
        "paidAmount": 200,
        "qbId": "1",
        "totalAmount": 212
      }
    ]
  },
  {
    "id": "fa5b77b5-bfd4-4178-ac31-386ec83f530c",
    "invoices": [
      {
        "description": "2022-01-12 - 95",
        "dueDate": "2022-01-12T14:08:26.219Z",
        "id": "05a58be3-4396-4c15-b9c2-ece68cb2b3fb",
        "invoiceDate": "2022-01-12T14:08:26.399Z",
        "openBalance": 7.33,
        "paidAmount": 7.33,
        "qbId": "",
        "totalAmount": 7.33
      },
      {
        "description": "2022-01-12 - 95",
        "dueDate": "2022-01-12T14:08:26.219Z",
        "id": "91f5ecd0-e18d-4029-8745-143323e02007",
        "invoiceDate": "2022-01-12T14:08:26.580Z",
        "openBalance": 53.13,
        "paidAmount": 53.13,
        "qbId": "",
        "totalAmount": 53.13
      }
    ]
  }
]

this jq will get me my ids jq '.QueryResponse.Invoice | map(.Id)' which can be readily consumed by jq. The question now is (and this is what I don't know) how to randomly choose from this array and update the sample payload:

jq 'map(. + {
   invoices : .invoices | map(. + {qbId: ??random here })
   })
'



Aucun commentaire:

Enregistrer un commentaire