0

I'm currently trying to filter a value from a long string I got by using a GET request with the cURL command. The output looks like this:

{"errors":null,"result":{"host_id":"1632","display_name":"notshownhere","hostname":"notshownhere","dnsname":"","ip":"1.1.1.1","host_type_id":"1","checks":[{"check_id":"12851","preset_id":"1","checktype_id":"1","checktype_short_name":"ping","status":"3","status_condition":"ok","status_color":"green","status_change_time":"1589066121","result_short":"1.1.1.1 is alive","result_time":"1591683892"}

My aim is to filter out the host_id from this output. This means, I want to find a way how I can only output the host_id with the number 1632.

I tried out Select-String, but it didn't work because the string is too long.

deralbert
  • 856
  • 2
  • 15
  • 33

1 Answers1

0

This looks like JSON and it would be best to use ConvertFrom-Json command on a proper JSON. That results in a PowerShell object where you could use object.'host_id' to retrieve the value.

Parsing the string, you can do the following.

# Assuming $j contains your string
($j | Select-String -Pattern '(?<="host_id":")\d+(?=")').Matches.Value

Since -pattern by default take a regex string, (?<=) contains a positive lookbehind from the current position. (?=) is a positive lookahead. \d+ is one or more digits. Since the command returns a MatchInfo object, you need to access the Value to return the digits.

AdminOfThings
  • 23,946
  • 4
  • 17
  • 27