Shane_IL Shane_IL - 4 months ago 10
JSON Question

Can I use grunt to set a property value in a json file?

Still learning the ropes of grunt and can't find a solution for this.
I have a config file, say config.json with some data.

When I run a specific grunt task I want to increment a value in the config.json file. I've been able to find a lot of information on how to read the file but nothing so far on changing a value.

Thanks.

Answer

you can use https://github.com/eruizdechavez/grunt-string-replace for replace string and save file

'string-replace': {
  dist: {
    files: {
      'dest/': 'src/**',
      'prod/': ['src/*.js', 'src/*.css'],
    },
    options: {
      replacements: [{
        pattern: /\/(asdf|qwer)\//ig,
        replacement: '"$1"'
      }, {
        pattern: ',',
        replacement: ';'
      }]
    }
  }
}

with this grunt plugin u can substitute a regex pattern (or a simple string) with a replacement

the steps for make your final json are: