从JQ列表中选择随机id并更新值 [英] Select random id from list in jq and update value

查看:4
本文介绍了从JQ列表中选择随机id并更新值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些要接收的样本负载,如下所示:

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

我知道这些是有效的,我需要在我收到的另一个有效负载中通过id交叉引用它们。但是,我无法假定我收到的数据具有有效的ID。

所以,我想从上面取出所有有效的Id,并将它们随机放入我拥有的样本数据中,如下所示($.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
      }
    ]
  }
]

此JQ将获取我的IDjq '.QueryResponse.Invoice | map(.Id)',JQ可以随时使用。现在的问题是(这是我不知道的)如何从这个数组中随机选择并更新样本负载:

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

推荐答案

这显示了如何在假定bash或足够类似bash的环境下,从数组中随机选择元素:

#!/bin/bash

< /dev/urandom tr -cd '0-9' | fold -w 1 | jq -MRnc '

# Output: a prn in range(0;$n) where $n is `.`
def prn:
  if . == 1 then 0
  else . as $n
  | ([1, (($n-1)|tostring|length)]|max) as $w
  | [limit($w; inputs)] | join("") | tonumber
  | if . < $n then . else ($n | prn) end
  end;

# Input: an array
# Output: an array, being a selection of $k elements from . chosen at random without replacement
def prns($k):
  if $k <= 0 then []
  else . as $in
  | length as $n
  | if $k > $n then "no can do" | error
    else ($n|prn) as $ix
    | [$in[$ix]] + (($in[0:$ix] + $in[$ix+1:])|prns($k-1))
    end
  end;


# Two illustrations
# Three from range(0,10) (with replacement):
[range(0;10) | ( ["a", "b", "c"] | .[length|prn]) ],

# Three from an array, without replacement:
([range(0;10)] | prns(3))

'

这篇关于从JQ列表中选择随机id并更新值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆