首页 > 解决方案 > 如何对大型 JSON 文件进行基于数学的批量更改?

问题描述

假设我有几个名称的目录,如下所示:

{
    "name": "Miller John",
    "mobile": "897654321",
    "age": 45,
    "salary": 120000,
    "address": {
        "city": "New York",
        "country": "USA"
    }
}

我现在想将每个人的工资字段除以k并将当前工资替换为该工资除以k。对于非常大的 JSON,我该怎么做?


编辑:

我正在处理的实际代码是一个包含多个项目的 NDJSON,其中第一个项目如下:

{"index":{"_id":1}}
{"name":"Wine - Maipo Valle Cabernet","price":152,"in_stock":38,"sold":47,"tags":["Alcohol","Wine"],"description":"Aliquam augue quam, sollicitudin vitae, consectetuer eget, rutrum at, lorem. Integer tincidunt ante vel ipsum. Praesent blandit lacinia erat. Vestibulum sed magna at nunc commodo placerat. Praesent blandit. Nam nulla. Integer pede justo, lacinia eget, tincidunt eget, tempus vel, pede. Morbi porttitor lorem id ligula.","is_active":true,"created":"2004\/05\/13"}

我正在尝试将价格字段除以 10。

标签: pythonjson

解决方案


您可以jq像这样在终端中使用:

jq '.salary= (.salary/1000)' YourFile.json

输出

{
  "name": "Miller John",
  "mobile": "897654321",
  "age": 45,
  "salary": 120,
  "address": {
    "city": "New York",
    "country": "USA"
  }
}

推荐阅读