JSON配列の数字から小数点桁を削除するには?

JSON配列の数字から小数点桁を削除するには?

bash次のように、JSONファイルの数値からすべての小数点以下の桁数を効果的に削除するためにスクリプトで実行できるシェルコマンドは何ですか?

    [
        {
            "IMSKU": "1000001", 
            "AttributeID": 7332.0, 
            "Value": "McAfee Host Intrusion Prevention for Desktops safeguards your business against complex security threats that may otherwise be unintentionally introduced or allowed by desktops and laptops. Host Intrusion Prevention for Desktops is easy to deploy, configure, and manage.", 
            "Unit": null, 
            "StoredValue": null, 
            "StoredUnit": null, 
            "Name": "Marketing text", 
            "Format": "1", 
            "Position": "1", 
            "Group_Name": "Basic Specification", 
            "AGGroup_Position": 0.0, 
            "Product_Hierarchy": 15198001453.0
        }, 
        {
            "IMSKU": "1000001", 
            "AttributeID": 7343.0, 
            "Value": "May 2013", 
            "Unit": null, 
            "StoredValue": null, 
            "StoredUnit": null, 
            "Name": "PI Date", 
            "Format": "1", 
            "Position": "1", 
            "Group_Name": "PI DATE", 
            "AGGroup_Position": 1.0, 
            "Product_Hierarchy": 15198001453.0
        }, 
        {
            "IMSKU": "1000001", 
            "AttributeID": 7344.0, 
            "Value": "McAfee", 
            "Unit": null, 
            "StoredValue": "0.00", 
            "StoredUnit": null, 
            "Name": "Brand Name", 
            "Format": "3", 
            "Position": "1", 
            "Group_Name": "PRODUCT", 
            "AGGroup_Position": 2.0, 
            "Product_Hierarchy": 15198001453.0
        }
    ]

〜する

"AttributeID":  7344.0

なります

"AttributeID":  7344

たとえば、など。

ベストアンサー1

IDフィルタで実行し、小数点をjq含む数字を整数.0に再フォーマットします。

$ jq . file.json
[
  {
    "IMSKU": "1000001",
    "AttributeID": 7332,
    "Value": "McAfee Host Intrusion Prevention for Desktops safeguards your business against complex security threats that may otherwise be unintentionally introduced or allowed by desktops and laptops. Host Intrusion Prevention for Desktops is easy to deploy, configure, and manage.",
    "Unit": null,
    "StoredValue": null,
    "StoredUnit": null,
    "Name": "Marketing text",
    "Format": "1",
    "Position": "1",
    "Group_Name": "Basic Specification",
    "AGGroup_Position": 0,
    "Product_Hierarchy": 15198001453
  },

(etc.)

小数点以下の桁数がゼロ以外の数字を持ち、その数字も削除するには、次のようにします。

jq '(.. | select(type == "number" )) |= floor' file.json

これはfloor、データのすべての数値に関数を適用し、最も近い整数に切り捨てます。

また、最後の点の後に数字を含む文字列があるかどうかを調べ、その数字(および点)を削除します。

jq '(.. | select(type == "string")) |= sub("\\.[0-9]+$"; "")' file.json

影響を受ける項目は依然として文字列であり、数値型に変換されません。

おすすめ記事