Skip to content

Instantly share code, notes, and snippets.

@luckylittle
Last active March 14, 2019 08:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save luckylittle/b4d08b508982c9996a0cd326a598f904 to your computer and use it in GitHub Desktop.
Save luckylittle/b4d08b508982c9996a0cd326a598f904 to your computer and use it in GitHub Desktop.
Closing the JSON properly in Ansible (replace last comma with curly braces)
# Sometimes there are moments, when you construct JSON in a really dirty way by just appending key/value pairs and adding comma at the end.
# You will end up with something like this:
# {
# "big_key": {
# "small_key1": "small_value1",
# "small_key2": "small_value2",
# "small_key3": "small_value3",
# And then to make this valid JSON, you need to replace the last comma with curly braces.
# This is an example how to do it in Ansible using regex in the replace module:
- name: Fix the broken JSON
replace:
path: "unfinished.json"
regexp: ',.*$\n\Z'
replace: '\n } \n}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment