首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在JSON数组中删除数字的小数位?

如何在JSON数组中删除数字的小数位?
EN

Unix & Linux用户
提问于 2019-12-22 17:19:43
回答 1查看 5K关注 0票数 4

可以在bash脚本中发出什么样的shell命令,以便有效地从JSON文件中的数字中删除所有十进制位,如下所示:

代码语言:javascript
复制
    [
        {
            "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
        }
    ]

所以

代码语言:javascript
复制
"AttributeID":  7344.0

会变成

代码语言:javascript
复制
"AttributeID":  7344

例如,等等。

EN

回答 1

Unix & Linux用户

回答已采纳

发布于 2019-12-22 17:38:24

只要使用jq在标识筛选器中运行它,就可以将具有.0小数点的数字重新格式化为整数:

代码语言:javascript
复制
$ 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.)

如果有小数的小数不是零,而且你也想删除这些,请使用

代码语言:javascript
复制
jq '(.. | select(type == "number" )) |= floor' file.json

这将对数据中的所有数字应用floor函数,将其舍入到最接近的整数。

还要研究是否有字符串包含在点后面的数字,并移除这些数字(和点):

代码语言:javascript
复制
jq '(.. | select(type == "string")) |= sub("\\.[0-9]+$"; "")' file.json

受影响的条目仍然是字符串,不会转换为数字类型。

票数 12
EN
页面原文内容由Unix & Linux提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://unix.stackexchange.com/questions/558510

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档