On 2020-09-13 18:40, Raghu Ram Baisani wrote:
Hi

I'm returning a JSON output from the puppet task which is written in PowerShell(.ps1) file.
The output returned from the task is a JSON value.
I have a plan in which I'm calling puppet task which is written in PowerShell and after getting the result I'm using parsejson function to parse the output received inside the plan.
*Example for the JSON returned by task:

*
{
     "height":  "5\\11",
     "name":  "Raghuram"
}
after calling parsejson on the above json string I received below object,
{
     "height":  "511",
     "name":  "Raghuram"
}
the slashes are getting removed in the parsed object of  JSON string.
After googling,
I used this Link <https://stackoverflow.com/questions/41290380/ruby-json-parse-stripping-escaped-back-slashes-from-string> and put three slashes in the input string.
*Input:*
{
     "height":  "5\\\11",
     "name":  "Raghuram"
}
then also  I got the same output with all the slashes removed.
I need any tricks/tips to mitigate this issue.
*Excepted Output:*
{
     "height":  "5\11",
     "name":  "Raghuram"
}


The thing with backslash escapes is that it is easy to be tricked by the representation the original string is written in, and the representation that it is presented in. For example if writing this in Ruby:

   a = "5\\11"

then a will be a string that has the characters 'a', `\', '1', '1' since the backslash is escaped it results in one. Now, if this is fed to json to be parsed it is thus given "5\11" and since "\1" is not a recognized escape sequence it just drops the backslash.

If you use three backslashes, the Ruby string "5\\\11" and parse that as JSON the parser gets "5\\11" which to JSON means the desired result of "5\11" - HOWEVER, if you look at that result in Ruby it presents that as a valid Ruby source string and it displays "5\\11" which may lead you to believe that it did not work.

Here is an example:

  irb(main):020:0> JSON.parse('{"foo": "5\\\11"}')["foo"].chars
  => ["5", "\\", "1", "1"]

as you can see the backslash char is shown as a string with two backslashes.

And if you do this:

  irb(main):021:0> a = JSON.parse('{"foo": "5\\\11"}')["foo"].bytes
  => [53, 92, 49, 49]

You see that it indeed only has one backslash char in the resulting string.


Hope this helps you.

Best,
- henrik

Thanks
Raghuram Baisani

--
You received this message because you are subscribed to the Google Groups "Puppet Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users+unsubscr...@googlegroups.com <mailto:puppet-users+unsubscr...@googlegroups.com>. To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-users/102a0324-ec2a-45c9-b54b-869f780e3328n%40googlegroups.com <https://groups.google.com/d/msgid/puppet-users/102a0324-ec2a-45c9-b54b-869f780e3328n%40googlegroups.com?utm_medium=email&utm_source=footer>.


--

Visit my Blog "Puppet on the Edge"
http://puppet-on-the-edge.blogspot.se/

--
You received this message because you are subscribed to the Google Groups "Puppet 
Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to puppet-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/puppet-users/rjm3bi%2413m%241%40ciao.gmane.io.

Reply via email to