Vlocity deployment returns error Unexpected token in JSON at position

When deploying Vlocity components, errors similar to the one below might be returned.

'Error Loading >> Product2/100-Minutes-and-Messages-Upgrade - Unexpected string in JSON at position 4655'

The errors "Unexpected token in JSON at position", are caused by an incorrect format of the Vlocity files with names ending with DataPack.json in the components mentioned in the errors.

The files are getting corrupted when Git perform the merges by itself without Copado intervention.

Git is designed to work with source code, where each line are easy to be identified as unique within a block of code. JSON and XML are formats that repeats the same line of “code” many times. This makes it very difficult for Git merge algorithms to handle it properly. As a result successfully merged XML or JSON files may end up with duplicate tags. 
Copado only intercepts git merges when there is a git conflict. Copado provides different strategies to be applied to auto-resolve a conflict, when Copado intercept a merge conflict it either use a parser to re-write files therefore file format is correct or the file is overwritten with a copy of the feature branch which is correct because it hasn’t been merged yet.
The problem we are describing in this document happens when a successful git merge, therefore not intercepted by Copado, produces an invalid file format.

How to workaround the issue:

Let's take the error above. The error means that 1 corrupted JSON files was found in the promotion branch when deploying. The file might not be related with the components you are deploying. You might also get more errors meaning that more files are corrupted.

The component affected is:

Product2/100-Minutes-and-Messages-Upgrade

In order to fix the error, you must navigate to the promotion branch for that deployment, find the component in the branch, open the DataPack.json file for that components and fix the file.

In order to know what is wrong in the JSON files, you can copy and paste the file on this JSON Formatter Validator.
https://jsonformatter.curiousconcept.com/

Once the file is fixed on the promotion branch, you can click Deploy in the same deployment and the deployment should be completed successfully.

Note that if the files are corrupted in the promotion branch, it means they are also corrupted in the target branch of these deployments so ideally, you should fix the files in the main branches to prevent the issue from happening with any other deployment. If the files are fixed only in the promotion branch, the error will appear again in the next deployment to that environment.

Why are the errors returned for components you are not deploying?

When using the packDeploy command with the Vlocity Build Tool, there is a file that specifies the components to be deployed and the folder where the components are located. In the integration with Copado, the folder will contain all the Vlocity components in the Vlocity folder of the promotion branch. The Vlocity Build Tool process all the Datapack.json files in that folder to find dependencies of the components that are going to be deployed. When the files are processed, the error is returned.

Is there any final solution?

Since we cannot modify the way Git perform the merge and we do not want to overwrite files in the target by default when merging branches, we cannot prevent the files from being corrupted when git perform the merge by itself. This doesn’t mean that every single git merge is going to corrupt the files.

We are evaluating different ways of handling these errors to make sure they are displayed in a better way to make it easier for the customer to understand and resolve them.

How did we do?