0

I'm collecting data in raw text with RESTAPI from an appliance and I would like to replace a particular block of text from it.

I'm not very comfortable with types and I'm stuck on this since hours.

Let say here is my original text :

{
"type":  "V9",
"firmware":  {
                 "manageFirmware":  false,
                 "reapplyState":  "NotApplying"
             },
"inProgress":  false,
"connectionSettings":  {
                           "reapplyState":  "NotApplying",
                           "connections":  [
                                               "@{id=1; name=PR_1; state=Deployed; status=OK}",
                                               "@{id=2; name=PR_2; state=Deployed; status=OK}",
                                               "@{id=3; name=SN_1; state=Reserved; status=Unknown;}",
                                               "@{id=4; name=SN_2; state=Reserved; status=Unknown;}"   
                                           ]
                       },
"Tag":  "123456"}

I would like to replace the block after "connections" : :

[ "@{id=1; name=PR_1; state=Deployed; status=OK}",
  "@{id=2; name=PR_2; state=Deployed; status=OK}",
  "@{id=3; name=SN_1; state=Reserved; status=Unknown;}",
  "@{id=4; name=SN_2; state=Reserved; status=Unknown;}"
]

By another text block I'm currently able to collect, let say this one :

[
{
    "id":  1,
    "name":  "PR_1",
    "state":  "Deployed",
    "status":  "OK",
},
{
    "id":  2,
    "name":  "PR_2",
    "state":  "Deployed",
    "status":  "OK",
},
{
    "id":  3,
    "name":  "SN_1",
    "state":  "Reserved",
    "status":  "Unknown",
},
{
    "id":  4,
    "name":  "SN_2",
    "state":  "Reserved",
    "status":  "Unknown",
}
]

The goal for me is to remove the "@, replace = with :, and remove } at the end of the line.

I already tried something with the -Replace option but it was not very concluding.

Also tried with some Regex but I'm very struggling for this one.

1 Answer 1

1

looks like the supplied json was converted from a psobject to json with a depth of 2. this code will put it back into an object and parse the hashtables into objects for the connections property, then convert it back to json with a depth of 3, which should get what you're looking for

$json = @'
{
"type":  "V9",
"firmware":  {
                 "manageFirmware":  false,
                 "reapplyState":  "NotApplying"
             },
"inProgress":  false,
"connectionSettings":  {
                           "reapplyState":  "NotApplying",
                           "connections":  [
                                               "@{id=1; name=PR_1; state=Deployed; status=OK}",
                                               "@{id=2; name=PR_2; state=Deployed; status=OK}",
                                               "@{id=3; name=SN_1; state=Reserved; status=Unknown;}",
                                               "@{id=4; name=SN_2; state=Reserved; status=Unknown;}"   
                                           ]
                       },
"Tag":  "123456"}
'@

$obj = $json | ConvertFrom-Json

$obj.connectionSettings.connections = $obj.connectionSettings.connections | % {
    [pscustomobject](iex ($_ -replace '(?<=[=])([^;}]*)(?=[;}])', "'`$1'"))
}

$newjson = $obj | ConvertTo-Json -Depth 3

$newjson
Sign up to request clarification or add additional context in comments.

1 Comment

Perfect, the regex works like a charm ! Plus your answer lead me to the source problem so I modified the initial Convertto-json parameter with -Depth 3 to avoid using the regex.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.