Parsing a string in which data could be anything

Hi, I am running a curl command which would result in something like this:

{“data”: [“OS Disk”, “CPU Usage”, “Memory Usage”, “All Disks”]}

However, the values inside of the quotation marks could be different at any time.

I am trying to end up with variables for each of the above, for example… the next step would be for me to then query each of those. For instance, after I retrieve “CPU Usage” I will then be running another command to find out the details of the “CPU Usage” … can someone help? I know this is a regex thing but way above my knowledge!

Looks like JSON to me. Simplest way is probably just to use ConvertFrom-Json:

$curlOutput = '{"data": ["OS Disk", "CPU Usage", "Memory Usage", "All Disks"]}'
$object = $curlOutput | ConvertFrom-Json

foreach ($item in $
    # Do something with $item

I found this which works for the first value in the string:

if ($string -match ‘"(.+?)"’) {

In this case, $Matches[1] has the value “data”.
Now I need to keep going in the string to capture all of the values between quotes…
Can someone help, thanks!

Ok thanks Dave, let me give that a shot!

works like a charm, and so simple, thanks!

I need to remove one item in $ …
Once I have the array, how can I remove one of the members?
In other words, I want to remove the source: nrpe item:

source : nrpe
tag : {Windows_Physical, Microsoft_Windows_Server_2008_R2_Standard_, winvm}


Depends on where you want that removed. Just in your object in memory for some reason, or on the server itself? If it’s the latter, then it depends on how your web interface works.

Basically I am going to send information back to the host API using the json file.
However, though the information I received when executing the XGET, there are two extra fields which the API will not accept. The json data looks like this:
“source”: “I don’t want this”,
“tag”: [
“parents”: [

"address":  [
"url":  " ",
"notes":  " ",
"hostname":  "I don't want this",
"umiworker":  "the moon",
"hosttype":  "server"


So basically I need to exclude the source and hostname names and data. I will take the data minus the those two fields/data and send a curl statement back. Basically I am doing this entire exercise because the only way to change server information in this API is to do the XGET, delete the server, modify the IP address for instance, then PUT the information back using a curl statement again :slight_smile:


Gotcha. Probably something along these lines, then:

$newJsonData = $oldJsonData |
               ConvertFrom-Json |
               Select-Object -ExcludeProperty source,hostname |

Thanks Dave.
I had to modify your code just a bit:
$ = $ |
ConvertFrom-Json |
Select-Object -ExcludeProperty source,hostname |

For the last piece of the puzzle!
The data from the curl command looked like so:

‘{“data”: [“OS Disk”, “CPU Usage”, “Memory Usage”, “All Disks”]}’

I am able to remove a couple of fields

Now I need the format to be like to have the proper escape characters so that I can send it back to the API :slight_smile:

in other words the data above should end up looking like this for the curl command:

“{“data”: [“OS Disk”, “CPU Usage”, “Memory Usage”, “All Disks”]}”

Can help me with that last piece?
I have tried a few things and cannot get there from here!

Looks like you’re only escaping single quotes, so this might be enough:

$curlParameter = $ -replace '"', '\"'

(note: don’t worry about the set of double quotes around the whole string. When you call an executable like curl.exe, PowerShell will quote the arguments for you anyway.)

Hi Dave,
To recap:

normally I do:

$ServerData = ‘{“data”: {“source”: “nrpe”, “tag”: [“OS”], “parents”: [], “address”: [“”],“hostname”: “server”,“hosttype”: “server”}}’ | convertto-json

And the result in $serverData is perfect to use for curl:

“{“data”: {“source”: “nrpe”, “tag”: [“OS”], “parents”: [], “address”: [“”],“hostname”: “server”,“hosttype”: “server”}}”

In the new process I remove data by doing the following:

$ServerData = ‘{“data”: {“source”: “nrpe”, “tag”: [“OS”], “parents”: [], “address”: [“”],“hostname”: “server”,“hosttype”: “server”}}’ | convertto-json

$ServerJson = $ServerData | ConvertFrom-Json

$ = $ | Select-Object * -ExcludeProperty source, hostname | ConvertTo-Json

Now $ is in this format:

“tag”: [
“parents”: [

"address":  [
"hosttype":  "server"


The last step in this I need to do is convert this back to:

“{“data”: {“tag”: [“OS”], “parents”: [], “address”: [“”],“hosttype”: “server”}}”

I have been through the trials and tribulations of trying to replace charaters and such in the past which has not always worked, so I would really like to do this with a conversion of some sort.
I am just not clear on how I can get the $ back to the desired format with the escape characters.

Do you have any ideas?

Ah, gotcha. Use the -Compress switch when you call ConvertTo-Json and you should be fine. :slight_smile:

That was it, thanks! All of the suffering we Win admins have to endure when forcing our data into a Linux API, among other Linux tools we have to use…

In case others need help, here is the entire process.

Data coming from a curl call:

{“data”: {“source”: “nrpe”, “tag”: [“OS”], “parents”: [], “address”: [“”],“hostname”: “server”,“hosttype”: “server”}}’

Set a variable to manipulate the data:

$ServerData = ‘{“data”: {“source”: “nrpe”, “tag”: [“OS”], “parents”: [], “address”: [“”],“hostname”: “server”,“hosttype”: “server”}}’ | convertto-json

Ready to remove some data:

$ServerJson = $ServerData | ConvertFrom-Json

$ServerJson = $ | Select-Object * -ExcludeProperty source, hostname | ConvertTo-Json -compress

Now the data is compressed but I need the escape characters for curl:

$ServerJsonResult | convertto-json

Then for my situation I have to change the double quotes to single quotes.
$result = $result -replace ‘"{’, “’{”
$result = $result -replace ‘}"’, “}’”

Probably an easier way to do that but it works… :slight_smile:
voila, PUT the data back!

Thanks so much for your time Dave!

To finish the job :slight_smile:

I have another situation in which I need to get those array values and store them in variables.

$ has data that I also will need to separate out

So for each value in $, I need to set a variable called $tag for the first value, $tag1 for the second value, and so on.

I have tried a number of things, none of which work.

Can you lead me a bit?

I see that if I run:

$ | foreach-object {$}
write-host $

I can see each object.