Is there a way to format JSON commas that makes merge GUI tools happy?

I’m working with a large team on a project that has a lot of JSON configuration files. They get changed frequently, and, of course, there a lot of merge conflicts. Our devs use a bunch of different GUI merge tools to choose which lines to use in a merge.

A lot of our merges are resolved by “choose both lines” (see example below). It seems during a lot of these merges, the GUI merge tools don’t know they need to add a comma to make the result syntactically-correct JSON. Our devs have to add the comma in manually, and all-to-often, they forget to do so (again, see example below).

  • How do I disable being able to set the git commit message from the command line?
  • How to detect whether a Git server supports shallow cloning?
  • wrong git repo owner
  • manage multiple github repos from the same system and different user names
  • github http clone returns 'did you run git update-server-info on the server'
  • Programmatically printing git revision and checking for uncommitted changes
  • Branch A

    {
         "enabled": true,
         "apiUrl":"https://myapi.com/v1"
    }
    

    Branch B

    {
        "enabled": true,
        "clientID": "abc123123"
    }
    

    Merged Branch:

    {
         "enabled": true,
         "apiUrl":"https://myapi.com/v1" <=== oops, no comma
         "clientID": "abc123123"
    }
    

    Is there a way to format JSON to prevent this from happening? I’m thinking this way (see below – commas in between lines of properties) might solve the issue, but I’m not sure. It also seems to waste a lot of space.

    {
         "enabled": true
         ,
         "apiUrl":"https://myapi.com/v1"
         ,
         "clientID": "abc123123"
    }
    

    What are my options here, short of telling our staff to be a little more careful while resolving merge conflicts.

  • Preventing local .gitignore from being pushed to svn repo with git-svn
  • Can I version control my .bash_profile?
  • git cherry-pick branchless commit on remote
  • Setting Up a Git Prototype
  • Restoring deleted submodules
  • Delete multiple remote branches in git
  • 2 Solutions collect form web for “Is there a way to format JSON commas that makes merge GUI tools happy?”

    Unfortunately, it seems that the real issue here is human error 🙂

    Asking your staff to “be a little more careful” when making changes to the code is a good idea but it probably won’t prevent this issue in the future.
    I don’t think that there would be a way to automatically “fix” the conflicts since once the JSON is missing a comma it is not longer a valid JSON and might as well be a normal unstructured text file. Consider the possibility that what renders the JSON invalid is an actual mistake and not just a borked merge by missing a comma.

    One thing I can suggest is to implement a “pre-commit” git hook that would attempt to JSON.parse (or something similar) the files and if an error occurred, you can block the commit with an appropriate message.

    It should be noted that you are not actually preventing the errors from happening – rather you’ll be able to detect them and notify the users before the problematic changes are saved.

    For what it’s worth, your proposed solution is similar to the Haskell idiom of starting non-initial lines with commas.

    {
         "enabled": true
         ,"apiUrl":"https://myapi.com/v1"
         ,"clientID": "abc123123"
    }
    

    The benefit in this case is that it seems to cover the common case of multiple developers appending new keys, leaving the first line untouched.

    Git Baby is a git and github fan, let's start git clone.