Home > database >  I want to filter data from output in powershell script
I want to filter data from output in powershell script

Time:04-08

The output which is stored in my variable is written below line, Also in attached picture. Variable name is $Required_Cor.

OUTPUT

{   "AllowedHeaders": "",   "AllowedMethods": "PATCH",   "AllowedOrigins": "eeeeeeeeeeeeeeeeeeeeee",   "ExposedHeaders": "",   "MaxAgeInSeconds": 0,   "Rule": 5,   "Service": "blob" }

I want to get only AllowedOrigins value which is eeeeeeeeeeeeeeeeeeeeee. Can you please tell me how to filter this. I'm trying to filter this with sed command. Below is the command i'm using.

$Origin= $(sed -i "/^\AllowedOrigins:/s*/,/^\s*/ExposedHeaders/" $Required_Cor)

But facing some syntax error i guess. Kindly help me.

CodePudding user response:

Use PowerShell's ConvertFrom-Json cmdlet to convert the json document to a structured object:

$dataObject = $Required_Cor |ConvertFrom-Json
$allowedOrigins = $dataObject.AllowedOrigins

$allowedOrigins now contain the string value eeeeeeeeeeeeeeeeeeeeee

  • Related