如何更新大型JSON文件上的字段?

huangapple go评论60阅读模式
英文:

How to update fields on a large json file?

问题

{
"height": 2.5,
"status": "open",
"date": 1662645600000,
"batch": {
"food": {
"-NBml_1X3O1H6Yrkr3LN0": {
"qty": 5.35,
"date": 1663004201119
},
"-NBmlcwczvQSgQauMBky0": {
"qty": 5.65,
"date": 1663197837636
}
},
"growth": {
"-NBml_1X3O1H6Yrr3LN0": {
"rate": 7.35,
"date": 1663004201219
},
"-NBmlcwczvQSQauMBky2": {
"rate": 5.4,
"date": 1663197837936
}
},
"date": 1663197837037
}
}

英文:

I have a large json file like this:

{
  "height": 2.5,
  "status": "open",
  "date": 1662645600000,
  "batch": {
    "food": {
      "-NBml_1X3O1H6Yrkr3LN0": {
        "qty": 5.35,
        "date": 1663004201119
      },
      "-NBmlcwczvQSgQauMBky0": {
        "qty": 5.65,
        "date": 1663197837636
      }
    },
    "growth": {
      "-NBml_1X3O1H6Yrr3LN0": {
        "rate": 7.35,
        "date": 1663004201219
      },
      "-NBmlcwczvQSQauMBky2": {
        "rate": 5.4,
        "date": 1663197837936
      }
    },
    "date": 1663197837037
  }
}

I would like to add 864000000 (10 days) to each date field

How can I do that using vscode, jsoneditor or a simple dart code?

答案1

得分: 1

你可以使用一个非常简单的Dart程序来实现。它将读取文件,进行json解码,递归查找正确的键,并将所需的金额添加到每个日期值。或者,你可以使用Dart中的正则表达式来解决,这更简单:

String data = File('your_json_file').readAsStringSync();
RegExp pattern = RegExp(r'"date":\s*(\d+)');
String newData = data.replaceAllMapped(pattern, (m) {
  int value = int.parse(m.group(1)!);
  int newValue = value + 864000000;
  return '"date": $newValue';
});

File('edited_json_file').writeAsStringSync(newData);

你也可以使用一个简单的sed命令来做同样的事情。这应该可以工作(但未经验证):

sed -E 's/"date": ([0-9]+)/echo "\"date\": \"$((+864000000))/eg' your_json_file > edited_json_file
英文:

You can do that with a very simple dart program. It would read the file, jsonDecode it, recursively find the correct key, and add the desired amount to each date value. OR you could solve it in dart using a regex, which is even simpler:

String data = File('your_json_file').readAsStringSync();
RegExp pattern = RegExp(r'"date":\s*(\d+)');
String newData = data.replaceAllMapped(pattern, (m) {
  int value = int.parse(m.group(1)!);
  int newValue = value + 864000000;
  return '"date": $newValue';
});

File('edited_json_file').writeAsStringSync(newData);

You could do the same with a simple sed command. This should work (but not verified):

sed -E 's/"date": ([0-9]+)/echo "\"date\": "$((+864000000))/eg' your_json_file > edited_json_file

答案2

得分: 0

以下是已翻译好的内容:

使用扩展 Regex Text Generator

  • 在需要更新的每个 "data": 文本上放置多光标选择。您可以使用全局查找和 Alt+Enter 或任何其他您喜欢的方法来执行此操作。
  • 使用以下步骤选择日期数字:ArrowRight ArrowRight Shift+End
  • 执行命令:基于正则表达式(regex)生成文本
  • 使用原始正则表达式:(.*)
  • 使用生成器正则表达式:{{=N[1]+864000000}}
    • 如果您想要预览,请在生成器正则表达式的输入框中按 Enter
    • 如果不是您想要的,请在生成器正则表达式的输入框中按 Esc
  • Esc 退出多光标模式
英文:

With the extension Regex Text Generator

  • place Multi Cursor Selection on every "data": text that needs updating. You can do that globally with Find and Alt+Enter or any other method you like
  • select the date numbers with: ArrowRight ArrowRight Shift+End
  • execute command: Generate text based on Regular Expression (regex)
  • use original regex: (.*)
  • use generator regex: {{=N[1]+864000000}}
    • if you like preview press Enter in input box of generator regex
    • if not what you want press Esc in input box of generator regex
  • press Esc to leave Multi Cursor Mode

答案3

得分: 0

你可以在VSCode中使用一个能够轻松进行数学运算的扩展,Find and Transform(由我编写)。使用类似以下的按键绑定:

{
  "key": "alt+a",                    // 任何你想要的按键绑定
  "command": "findInCurrentFile",
  "args": {
    "description": "为每个日期字段添加864000000",
    "find": "(?<=\"date\":\\s*)(\\d+)",
    "replace": "$${ return $1 + 864000000; }$$",
    "isRegex": true
  }
}

如何更新大型JSON文件上的字段?

英文:

You can do that in vscode with an extension that can do math easily, Find and Transform (which I wrote). With a keybinding like this:

{
  &quot;key&quot;: &quot;alt+a&quot;,                    // whatever keybinding you want
  &quot;command&quot;: &quot;findInCurrentFile&quot;,
  &quot;args&quot;: {
    &quot;description&quot;: &quot;add 864000000 to each date field&quot;,
    &quot;find&quot;: &quot;(?&lt;=\&quot;date\&quot;:\\s*)(\\d+)&quot;,
    &quot;replace&quot;: &quot;$${ return $1 + 864000000; }$$&quot;,
    &quot;isRegex&quot;: true
  }
}

如何更新大型JSON文件上的字段?

答案4

得分: 0

你可以使用JsonEditorOnline.org的“Transform”功能。在Lodash查询框中,你可以使用以下函数:

function query (data) {
  const newData = _.cloneDeep(data);
  const numDias = 10;

  const iterate = (obj) => {
    _.forOwn(obj, (value, key) => {
      if (key === "date") {
        obj[key] = value + numDias*24*60*60*1000;
      } else if (_.isObject(value)) {
        iterate(value);
      }
    });
  };

  iterate(newData);

  return newData;
}

它会将10天添加到每个Unix时间戳的“date”字段。

英文:

You can use the "Transform" Feature of JsonEditorOnline.org. On the Lodash Query box, you can use the following function:

function query (data) {
  const newData = _.cloneDeep(data);
  const numDias = 10;

  const iterate = (obj) =&gt; {
    _.forOwn(obj, (value, key) =&gt; {
      if (key === &quot;date&quot;) {
        obj[key] = value + numDias*24*60*60*1000;
      } else if (_.isObject(value)) {
        iterate(value);
      }
    });
  };

  iterate(newData);

  return newData;
}

It will sum 10 days to each unix timestamp date field

huangapple
  • 本文由 发表于 2023年4月19日 17:52:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76053107.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定