grunt-update-json icon indicating copy to clipboard operation
grunt-update-json copied to clipboard

Option to clean field before merging?

Open petetnt opened this issue 10 years ago • 3 comments

Hi,

I'm currently using grunt-update-json to update a certain json file with the contents of another file and it works great. However, I am having an issue where certain nodes have varying amount of key-value pairs. For instance, if I had this mark up:

{
  "thing1" : {
    "stuff" : "old"
  },
  "thing2" : {
    "stuff" : "old"
  },
  "thing3" : [{
      "some" : "old",
      "random" : "old",
      "stuff" : "old",
      "banana" : "old"
    }]
}

The structure of thing1 and 2 stays the same, but "thing3" is updated from another file, say thing3.json which looks like this:

{
  "thing3" : [{
   "some" : "new",
   "random" : "new",
   "stuff" : "new"
  }]
}

So now that the value with key banana is missing, the merged end product will look like this:

{
  "thing1" : {
    "stuff" : "old"
  },
  "thing2" : {
    "stuff" : "old"
  },
  "thing3" : [{
      "some" : "new",
      "random" : "new",
      "stuff" : "new",
      "banana" : "old"
    }]
}

While the wanted end result would be just the keys with value "new". Am I missing something or could there be an option to clean the branch before merging?

For example something like

bower: {
  src: 'package.json',    // where to read from
  dest: 'bower.json',     // where to write to
  clean: "version", //clean field "version" before merging
  // the fields to update, as a String Grouping
  fields: 'name version description repository'
}

petetnt avatar Oct 02 '14 10:10 petetnt

Hi @petetnt , I'm very excited about your and many other use cases for grunt-update-json that I would never have run into because my original requirements for it were so very minimal :)

My time is just super limited, so without any judgement, it's unlikely that I can contribute the time needed, but encourage you to submit a PR.

I see three quick fixes:

  • in some complicated cases it may make sense to use a template engine and just create the file from scratch (not sure if this fits your needs)
  • fork the repo and build the feature into your fork
  • possibly submit a PR

Thanks! Best,A.

AndreasPizsa avatar Oct 02 '14 11:10 AndreasPizsa

Thanks for the prompt reply! The use case here is quite limited, but if I get around to implement it myself, I'll shoot you with a PR :)

petetnt avatar Oct 03 '14 10:10 petetnt

+1 This should be added. I have also this scenario, where is necessary to clean some fields.

panzerdp avatar Aug 26 '15 14:08 panzerdp