humanmade / cf-to-tf

CLI tool for generating Terraform configuration and state for existing CloudFormation resources
Other
416 stars 70 forks source link

strip trailing commas in JSON lists/objects #7

Closed zackse closed 5 years ago

zackse commented 5 years ago

CloudFormation templates accept a "loose" JSON structure where trailing commas in lists/objects are allowed, but this fails when processing the source content with JSON.parse:

SyntaxError: Unexpected token } in JSON at position X at JSON.parse (<anonymous>)
at CF.getTemplate.promise.then.result (.../cf-to-tf/lib/template.js:9:24)
at process._tickCallback (internal/process/next_tick.js:68:7)

This crude hack attempts to strip trailing commas before passing the template body to JSON.parse.

If you are using @nathanielks 's scripts from https://gist.github.com/nathanielks/9282d59ab065f8ee0e373ca7199df085, the error would manifest as:

parse error: Invalid numeric literal ...

when the (error) output from cf-to-tf is passed to jq at https://gist.github.com/nathanielks/9282d59ab065f8ee0e373ca7199df085#file-import-stack-sh-L52

Hope this helps anyone else who runs into this.

nathanielks commented 5 years ago

@zackse thank you so much for contributing, this looks great 👍

Would you mind writing a test covering this case?

zackse commented 5 years ago

Hi @nathanielks, I committed a simple test case for lib/template.js -- let me know if this is sufficient or if you had something more exhaustive in mind.

Thanks for publishing this project!