Replace JSON Object Programmatically

I am looking for the most efficient way to replace a JSON object in a file.

20150628 - Update at the bottom of this post

Here's the script:

I have a bunch of JSON files (many) and these files have large chunks of JSON (sometimes 20-30K lines). These are the configurations for the various testing procedures we have. Recently, changing an object required a change:

"createdAt": {
    "year": 2014,
    "month": 11,
    "dayOfMonth": 24,
    "hourOfDay": 2,
    "minute": 22,
    "second": 54
}

      

in the following format:

"createdAt":"2015-05-12T21:14:51Z"

      

Let it even simplify. I want to replace all fields createdAt

and updatedAt

in my JSON object (of which there can be many) with

   "createdAt":"2015-05-12T21:14:51Z"

      

or

   "updatedAt":"2015-05-12T21:14:51Z"

      

Each file has NUMEROUS (100 of these) objects with different field values. I need to go through and replace each createdAt

and every updatedAt

object with a new format. The date doesn't matter. I can make them be anything.

I can do it manually, but it will literally take me a day or two to do a full day (I know I tried to make one file and after half an hour I gave up, it took way too long).

How can I do this programmatically?

Regex? Sed? Something other?

Final note: I only need to do this once. After that, I no longer need to do this.

Thanks for any advice!

JSON example: (Imagine the real one is 30,000 lines!) :)

{ "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
              }
            }
          }
        ]
}

      

UPDATE 20150628

For those wondering, here is this gulpfile I wrote to accomplish exactly what I wanted. It is based on the accepted answer. It will recursively search the tree for what I am looking for in order to replace it when found. It's not the most beautiful thing in the world, but it did exactly what I needed and saved me a couple of weeks of handcrafting. The total time to process all my files? Up to 100 ms. Amazing.

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/'))
});

      

+3


source to share


3 answers


Here's the kickoff. You implement your own date parsing logic. It requires gulp to be installed. And save this in gulpfile.js. You will need to iterate over all properties that are "date" objects. But this logic is not that difficult.



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/'))
});

      

+1


source


Why not manually?



   function formatDate(dateObject){
       var formattedDate = 
                  dateObject['year']+'-'+
                  dateObject['month']+'-'+ 
                  dateObject['dayOfMonth']+'T'+
                  dateObject['hourOfDay']+':'+
                  dateObject['minute']+':'+
                  dateObject['second']+'Z';
    }
    var jsonArray = {...};

    for(var key in jsonArray){
        for(var i = 0; i < jsonArray[key].length; i++){
            jsonArray[key][i]['createdAt'] = formatDate(jsonArray[key]['createdAt']); 
            jsonArray[key][i]['updatedAt'] = formatDate(jsonArray[key]['updatedAt']); 

    }
}

      

0


source


Open each file, change the property with the transform function, and then save the new JSON:

function changeDate(obj) {
  var newObject = obj.year + '-' + obj.month + '-' + obj.dayOfMonth + 'T' + obj.hourOfDay + ':' + obj.minute + ':' + obj.second;
  return newObject;
}

// here you open the file and stores it content in the products variable.
for (var i = 0; i < products.length; i++) {
  var product = products[i];
  product.product.createdAt = changeDate(product.product.createdAt);
  product.product.updatedAt = changeDate(product.product.updatedAt);
}
// .. now you need to save the modified json

      

0


source







All Articles