Extract string from json

Greetings all,

I’m trying to use the google translate api to translate words and save the content as a text or csv.

I’m having a hard time extracting the json content returned from invoke-Restmethod. It appears the translated data is in the “SyncRoot” property.

Any help would be appreciated

 

$Text = "Hi there!", "My name is John.", "What is your name?"," Cool!"
$TargetLanguage = "es"
$Uri = "https://translate.googleapis.com/translate_a/single?client=gtx&sl=auto&tl=$($TargetLanguage)&dt=t&q=$Text".
$Response = Invoke-RestMethod -Uri $Uri -Method Get
$Response.Content

Admittedly, I’m very unfamiliar with this API. Having said that Invoke-RestMethod is already parsing the JSON into objects. It appears to me that the first object’s syncroot property has all the text you are looking for but interspersed with integers. So you can do this to get the data you want in an array of strings:

$Response[0] | 
    Select-Object -ExpandProperty syncroot | 
        Where-Object {$_.gettype().Name -eq "String"}

 

$Text = "Hi there!", "My name is John.", "What is your name?"," Cool!"
$TargetLanguage = "es"
$Uri = "https://translate.googleapis.com/translate_a/single?client=gtx&sl=auto&tl=$($TargetLanguage)&dt=t&q=$Text"
$Response = Invoke-RestMethod -Uri $Uri -Method Get

$Response[0].SyncRoot | foreach { $_[0] }