question

FoxHD avatar image
0 Votes"
FoxHD asked ·

Unable to split yml object

I have used convert to yaml and convert json to yaml now I have say $a=$b.path.s.values="//server/main/builds" I want to replace /with \but not happen as it's yaml element $a=$b.path.s.values.replace("/","\") not working for yaml element

 #the full code
 $c=c:\temp\my.json
 $b=gc $c -raw | out-string| convertfrom-json| convertto-yaml
 $b=$b.convertfrom-yaml
 $a=$b.path.s.values.replace("/","\")
 My json like
 {
 Path:
 S://server/mypath/subdir
 }


windows-server-powershell
· 1
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Further I have tried to split the object but not happened also I have checked with
$a=$b.path.s.values|gm

And replace method is there then why it's not executing

0 Votes 0 ·
RichMatheisen-8856 avatar image
0 Votes"
RichMatheisen-8856 answered ·

Why not deal with the output of ConvertFrom-Json and then, after making the substitution, send the object to the ConvertTo-Yaml?

BTW, your "My json like" example is incorrect. You have to quote the value "S://server/mypath/subdir" or the "S:" will be taken as a JSON primitive and the conversion will fail.

· Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

IanXue-MSFT avatar image
0 Votes"
IanXue-MSFT answered ·

Hi,

Please check if this works. The first backslash is the "excape" character that makes the second backslash be seen as an ordinary character.

 $c="c:\temp\my.json"
 $b=(gc $c -raw | out-string).replace("/","\\") | convertfrom-json| convertto-yaml

Best Regards,
Ian Xue
============================================
If the Answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


· 2 · Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

This is close actually it is replace all / in yml
I want for specific child element only say O or S here

{
Path:
"S": "//server/mypath/subdir",
sub:
"O":"//another server/nextpath/file"

}

0 Votes 0 ·

That bit of JSON isn't correct. Is this what you meant?

 {
     "Path":  {
                  "S":  "//server/mypath/subdir"
              },
     "sub":  {
                 "O":  "//another server/nextpath/file"
             }
 }
1 Vote 1 ·
RichMatheisen-8856 avatar image
0 Votes"
RichMatheisen-8856 answered ·

Adjust this example to suit your requirements. It doesn't use YAML but you can pipe the "ConvertTo-JSON" into you "ConvertTo-YAML".

 $h = [ordered]@{
     Path = [ordered]@{S = "//server/mypath/subdir"}
     sub =  [ordered]@{O = "//another server/nextpath/file"}
 }
 $j = $h | ConvertTo-Json
 $j  # show JSON
 "===== ^^    ======= || OUT =============================="
 "===== || IN ======= vv =================================="
 $p = $j | ConvertFrom-Json  # convert JSON to Powershell CustomObject
    
 $parent = "Path"
 $child  = "S"
 $p.$parent.$child = $p.$parent.$child -replace "/","\"
 $p | ConvertTo-Json         # all single "\" will be escaped, so "\" becomes "\\" and "\\" becomes "\\\\"


· Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.