以编程方式替换 JSON 对象

Don*_*ker 5 javascript regex json replace sed

我正在寻找最有效的方法来替换文件中的 JSON 对象。

20150628 - 这篇文章底部的更新

这是场景:

我有一堆 JSON 文件(很多),在这些文件中是大块的 JSON(有时是 20-30K 行)。这些是我们拥有的各种测试程序的配置。最近,需要进行更改才能更改对象:

"createdAt": {
    "year": 2014,
    "month": 11,
    "dayOfMonth": 24,
    "hourOfDay": 2,
    "minute": 22,
    "second": 54
}
Run Code Online (Sandbox Code Playgroud)

到这样的格式:

"createdAt":"2015-05-12T21:14:51Z"
Run Code Online (Sandbox Code Playgroud)

让我们更轻松。我想用以下内容替换我的 JSON 对象(可以有很多)中的所有createdAtupdatedAt字段:

   "createdAt":"2015-05-12T21:14:51Z"
Run Code Online (Sandbox Code Playgroud)

或者

   "updatedAt":"2015-05-12T21:14:51Z"
Run Code Online (Sandbox Code Playgroud)

每个文件中有 NUMEROUS 个(其中 100 个)对象,字段的值不同。我需要经过和更换每一个createdAtupdatedAt使用新的格式对象。日期无所谓。我可以让它们成为任何东西。

我可以手动完成这项工作,但实际上我需要一两天的时间才能完成全职工作(我知道,我尝试做一个文件,1/2 小时后我放弃了,时间太长了) .

如何以编程方式执行此操作?

正则表达式?塞德?还有什么?

最后一点:我只需要这样做一次。在那之后,我就不需要再这样做了。

感谢您提供任何提示!

JSON 示例:(想象一下真正的 30,000 行!):)

{ "products": [
          {
            "displayOrder": 3,
            "product": {
              "foo": "bar",
              "createdAt": {
                "year": 2014,
                "month": 11,
                "dayOfMonth": 24,
                "hourOfDay": 2,
                "minute": 22,
                "second": 54
              },
              "description": "Fizz Bin",
              "id": "8765309",
              "modelNumber": "call-it",
              "name": "Boom",
              "price": {
                "amount": 100,
                "currency": "USD"
              },
              "type": "Active",
              "updatedAt": {
                "year": 2015,
                "month": 1,
                "dayOfMonth": 27,
                "hourOfDay": 19,
                "minute": 33,
                "second": 25
              }
            }
          },
          {
            "displayOrder": 4,
            "product": {
              "foo": "barx",
              "createdAt": {
                "year": 2013,
                "month": 1,
                "dayOfMonth": 4,
                "hourOfDay": 3,
                "minute": 2,
                "second": 5
              },
              "description": "Fizzy Stuff",
              "id": "876511111",
              "modelNumber": "zoom-zoom-1000",
              "name": "Zoom Zoom 1000",
              "price": {
                "amount": 1000,
                "currency": "USD"
              },
              "type": "Active",
              "updatedAt": {
                "year": 2011,
                "month": 5,
                "dayOfMonth": 25,
                "hourOfDay": 15,
                "minute": 35,
                "second": 55
              }
            }
          }
        ]
}
Run Code Online (Sandbox Code Playgroud)

更新 20150628

对于那些想知道的人,这是我为完成我想要的而编写的 gulpfile。它基于接受的答案。它将递归搜索树以查找我正在寻找的内容,并在找到时替换它。它不是世界上最漂亮的东西,但它确实满足了我的需求,并为我节省了几个星期的手动时间。处理我所有文件的总时间?低于 100 毫秒。惊人的。

var gulp = require('gulp');
var change = require('gulp-change');

function searchTreeForDates(obj) {
    if(typeof(obj) === 'object') {
        for (var key in obj) {
            if (typeof(obj[key]) === 'object' && (key === 'createdAt' || key === 'updatedAt')) {
                obj[key] = "2015-06-29T00:53:00Z";
            } else {
                obj[key] = searchTreeForDates(obj[key])
            }
        }
    }
    return obj; 
}

function updateDate(content) {
    var obj = JSON.parse(content);
    obj = searchTreeForDates(obj);
    return JSON.stringify(obj);
}

gulp.task('change', function() {
    return gulp.src('*.json')
        .pipe(change(updateDate))
        .pipe(gulp.dest('changed/'))
});
Run Code Online (Sandbox Code Playgroud)

Nix*_*Nix 2

这是一个初步的尝试。您实现自己的“日期解析逻辑”。它需要你安装 gulp。并将其保存在 gulpfile.js 中。您可能需要循环遍历所有“日期”对象的属性。但这个逻辑并不难。

var gulp = require('gulp');
var change = require('change');

function translateDate(dateField){
    return dateField.A + dateField.b + ...;

}
function updateDate(content) {
    var obj = JSON.parse(content);
    //loop over the obj properties and call the below
    // for the ones you want to change.
    obj.dateField = translateDate(obj.dateField);
    return JSON.stringify(obj);
}

gulp.task('change', function() {
    return gulp.src('**/*.json')
      .pipe(change(updateDate))
      .pipe(gulp.dest('changed/'))
});
Run Code Online (Sandbox Code Playgroud)