# Powershell modules, json conversion quirk

**URL:** <https://forum.ansible.com/t/powershell-modules-json-conversion-quirk/18193>\
**Category:** Ansible Developer\
**Tags:** windows\
**Created:** [May 8, 2015, 5:36pm UTC](https://forum.ansible.com/t/powershell-modules-json-conversion-quirk/18193 "2015-05-08T17:36:24Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![j.r.hawkesworth](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@j.r.hawkesworth](https://forum.ansible.com/u/j.r.hawkesworth)\
**Post date:** [May 8, 2015, 5:36pm UTC](https://forum.ansible.com/t/powershell-modules-json-conversion-quirk/18193/1 "2015-05-08T17:36:24Z")

</div>

I came across this powershell quirk today and thought I should share it with anyone else who might be writing powershell ansible modules.

It seems there is a bug, or at least an inconsistency in the behaviour of ConvertTo-Json

It appears that it fails to convert strings to json if they end with a trailing backslash, which is a hazard for windows folder paths which can be expressed with a trailing backslash, e.g c:\temp\

What is strange is that if you use the -Compress option with ConvertTo-Json the string seems to convert ok.

I wound up using

$message = $message.TrimEnd("") to clean up the affected string in my custom module, but I’m considering whether it would be wise to add -Compress to the two places it occurs in lib/ansible/module\_utils/powershell.ps1. This would make ansible a bit more robust to modules without sanitized output - I suppose at the expense of the overhead of ‘compressing’ the json (which seems to just be quoting and removing whitespace).

The issue is described here:

[https://connect.microsoft.com/PowerShell/feedback/details/869768/powershell-convertto-json-error-when-has-string-with-trailing-backslash](https://connect.microsoft.com/PowerShell/feedback/details/869768/powershell-convertto-json-error-when-has-string-with-trailing-backslash)

All the best,

Jon

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [May 8, 2015, 5:52pm UTC](https://forum.ansible.com/t/powershell-modules-json-conversion-quirk/18193/2 "2015-05-08T17:52:48Z")

</div>

i would be fine with always compressing the json, most of the time we  
reformat the output at the ansible command line anyways.

---

<div class="post-metadata">

**Author:** ![Ivaylo\_Bratoev](https://avatars.discourse-cdn.com/v4/letter/i/f9ae1b/32.png) [@Ivaylo\_Bratoev](https://forum.ansible.com/u/Ivaylo_Bratoev)\
**Post date:** [May 10, 2015, 12:28am UTC](https://forum.ansible.com/t/powershell-modules-json-conversion-quirk/18193/3 "2015-05-10T00:28:05Z")

</div>

We use the _-Compress_ option for some of our custom modules and we have not have issues for now.

+1 for making it by default so that trailing backslash and some other issues are fixed.

---

<div class="post-metadata">

**Author:** ![j.r.hawkesworth](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@j.r.hawkesworth](https://forum.ansible.com/u/j.r.hawkesworth)\
**Post date:** [May 12, 2015, 8:41pm UTC](https://forum.ansible.com/t/powershell-modules-json-conversion-quirk/18193/4 "2015-05-12T20:41:24Z")

</div>

Ok. I was about to create PR to make the change but noticed that  
version of module\_utils/powershell.ps1 in v2 is older than the v1 code.

I’m thinking perhaps best to leave this until v2 is current.

Jon

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [May 27, 2015, 4:19pm UTC](https://forum.ansible.com/t/powershell-modules-json-conversion-quirk/18193/5 "2015-05-27T16:19:41Z")

</div>

files seem to be the same now, feel free to update one and we'll make  
sure changes percolate to the other.

---

<div class="post-metadata">

**Author:** ![j.r.hawkesworth](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@j.r.hawkesworth](https://forum.ansible.com/u/j.r.hawkesworth)\
**Post date:** [May 28, 2015, 7:03am UTC](https://forum.ansible.com/t/powershell-modules-json-conversion-quirk/18193/6 "2015-05-28T07:03:10Z")

</div>

Will do

---

<div class="post-metadata">

**Author:** ![Jon\_Hawkesworth](https://avatars.discourse-cdn.com/v4/letter/j/ed655f/32.png) [@Jon\_Hawkesworth](https://forum.ansible.com/u/Jon_Hawkesworth)\
**Post date:** [May 29, 2015, 1:59pm UTC](https://forum.ansible.com/t/powershell-modules-json-conversion-quirk/18193/7 "2015-05-29T13:59:09Z")

</div>

Still seems to be a small difference to me (different checksum algorithm being used), but I have pushed to the ‘v1’ version PR is [https://github.com/ansible/ansible/pull/11090](https://github.com/ansible/ansible/pull/11090)

Jon
