WinArchitect
WinArchitect

Reputation: 15

Problem replacing brackets in concatenated JSON formatted string with -replace and REGEX

I have a script that gets a JSON list from an API. The call to the API will only return 100 records so I use a while loop to do multiple calls and then append each responses into a string variable $rawData. Once the loop is done, I end up with improper brackets at every connection of two append actions and it ends up looking like this:

    }
  }
]
[
  {

Properly formatted, it should look like this:

    }
  },
  {

I have tried the following:

$rawData -replace "\s\s\}\]\[",','
$rawData -replace "\s\s\}\n\]\[",','
$rawData -replace "\s\s\}`n\]\[",','
$rawData -replace "\}\]\[",','
$rawData -replace "\}`n\]\[",','
$rawData -replace "\}\n\]\[",','

All of these return the following, which totally baffles me:

    }
  }
][
  {

Where is the comma that was specified as the replacement? How did it remove the newline after the ]? Then I tried to simplify it using:

$rawData -replace "\]\[",','

Output:

    }
  }
,
  {

So close! I am assuming that there is a newline there but I can't figure out how to get rid of it. I have scoured so many sites and am now completely frustrated. If possible, can you tell me the right syntax and why my attempts have failed?

----EDIT----

Thank you MClayton! That solved the issue and it is definitely more efficient. I'm still trying to grasp what is happening with the ConvertTo(From)-Json cmdlets. I am still very interested in why the -replace did not work and how to make it work for future reference. If anyone has an answer for that it would be very helpful.

Upvotes: 1

Views: 58

Answers (1)

mclayton
mclayton

Reputation: 9975

To summarise (and fill in a few blanks), you're basically doing something like this:

$response1 = @"
[
    { "object": { "name": "object 1"} },
    { "object": { "name": "object 2"} },
    { "object": { "name": "object 3"} }
]
"@

$response2 = @"
[
    { "object": { "name": "object 4"} },
    { "object": { "name": "object 5"} },
    { "object": { "name": "object 6"} }
]
"@

$result = $response1 + $response2;

which is giving a result like:

[
    { "object": { "name": "object 1"} },
    { "object": { "name": "object 2"} },
    { "object": { "name": "object 3"} }
][
    { "object": { "name": "object 4"} },
    { "object": { "name": "object 5"} },
    { "object": { "name": "object 6"} }
]

and that isn't valid json.

Rather than try to patch up the text with a regex, the suggestion by @Colyn1337 is to parse the json, join the objects and then turn it back into text:

$result = @( $response1, $response2 ) | convertFrom-Json
$result

#object
#------
#@{name=object 1}
#@{name=object 2}
#@{name=object 3}
#@{name=object 4}
#@{name=object 5}
#@{name=object 6}

Now you can either work on the objects directly, or you can turn it back into a single valid json string:

$json = $result | ConvertTo-Json

$json

#[
#  {
#    "object": {
#      "name": "object 1"
#    }
#  },
#  ... etc...
#  {
#    "object": {
#      "name": "object 6"
#    }
#  }
#]

Upvotes: 2

Related Questions