skip to Main Content

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?

4

Answers


  1. Chosen as BEST ANSWER

    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


  2. 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
    Login or Signup to reply.
  3. 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
    
    Login or Signup to reply.
  4. 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search