Skip to content

Instantly share code, notes, and snippets.

@Flogex
Last active April 8, 2023 19:11
Show Gist options
  • Save Flogex/e4e0bf24cdb027bc786a9bedc81abbe0 to your computer and use it in GitHub Desktop.
Save Flogex/e4e0bf24cdb027bc786a9bedc81abbe0 to your computer and use it in GitHub Desktop.
git diff for Postman Collections

git diff for Postman Collections

The Problem

When exporting a Postman Collection, the body of a request is stored in the "raw" property as a single string. When using git diff, it is hard to compare the changes because the whole line has changed.

Using jq as textconv

It is possible to define in a .gitattributes file what diff driver to use. For every driver, a command can be specified that converts the input file before the LCS algorithm is executed. That is, we can use Git's feature of diffing but alter how the single lines are generated.

Note: The following snippets are written for Git on Windows machines.

In the first step, create a new "postman" diff driver by adding the following line to git config:

[diff "postman"]
	textconv=jq \"(.item[].request.body?.raw | select (.)) |= (. | select ((. | length) > 0) | fromjson)\"
	cachetextconv = true

You can also achieve this by running git config diff.postman.textconv "jq \"(.item[].request.body?.raw | select (.)) |= (. | select ((. | length) > 0) | fromjson)\"" and git config diff.postman.cachetextconv true.

Specify the diff driver to use in the .gitattributes:

*.postman_collection.json diff=postman

Lastly, download jq and add it to the cmd folder in your Git directoy.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment