0

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?

David L
  • 1,134
  • 7
  • 35

4 Answers4

1

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\": "$((\1+864000000))/eg' your_json_file > edited_json_file
Robert Sandberg
  • 6,832
  • 2
  • 12
  • 30
  • Thanks @Robert. I'm testing the sed approach. I think it's a little bug but I don't know how to fix it. When I applied the SED command, the date change from 1663004201119 (a time stamp) to 1663004201119+864000000 (an string instead the sum as a new time stamp) – David L Apr 24 '23 at 02:33
  • I've updated the answer. Try that instead. ("works for me" ) :) – Robert Sandberg Apr 24 '23 at 08:48
  • Thanks. An error `bad flag in substitute command: e` appears. If I use /g only it doesn't work (file is corrupted with echo, $ and other characters inside the field. – David L Apr 24 '23 at 12:39
  • No you need the `e` because that makes sure to execute/evaluate the expression that is supposed to be the replacement. Not sure how to help you further. Maybe a version thing? You can always go with the simple dart program I wrote above as well. I'll edit the answer to say _should_ work with the sed command, since you didn't get it working. – Robert Sandberg Apr 24 '23 at 12:58
0

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
rioV8
  • 24,506
  • 3
  • 32
  • 49
0

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:

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

adding time to the date fields

Mark
  • 143,421
  • 24
  • 428
  • 436
0

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) => {
    _.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;
}

It will sum 10 days to each unix timestamp date field

David L
  • 1,134
  • 7
  • 35