Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to format json string for aws with powershell

i want to send an event to aws via a cli command in a powershell script. Here is the Json i need to send to the eventbridge:

   [
       {
          "Sensor":"{\"id\":\"880/2021-04-13\",\"attributes\":\"green\",\"Name\":\"SensorGreen\",\"state\":\"SUCCEEDED\"}",
          "Source":"google.com"
       }

    ]

Thats what a tried in powershell:

$json='[{"Sensor":"{\"id\":\"880/2021-04-13\",\"attributes\":\"green\",\"Name\":\"SensorGreen\",\"state\":\"SUCCEEDED\"}","Source":"google.com"}]'|ConvertTo-Json -Compress

aws events put-events --entries $json --region "eu-central-1"  

That does not work. I even tried to write it to a json file and read it and send it from the file but it doesnt work. It somehow leads to too many "\" slashes or no slashes for my "Sensor" where it is necessary. I even tried to create a object and just convert the Sensor object to Json and then the whole object again to have the escaping, but it is not working.

EDIT:

i tried also this as mentioned in the answer:
$RequestObject = [pscustomobject] @(@{
    Sensor = [pscustomobject] @{
        id = "880/2021-04-13"
        attribute = "green"
        Name = "SensorGreen"
        state = "SUCCEEDED"
    }
    Source = "google.com"
})
$RequestObject.Get(0).Sensor=$RequestObject.Get(0).Sensor | ConvertTo-Json -Compress
$Json = $RequestObject | ConvertTo-Json -Compress
aws events put-events --entries $Json --region "eu-central-1"

i included the @() to have an array and converted the sensor object twice to json to include the slashes. but the result is:

Error parsing parameter '--entries': Invalid JSON: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)

if i use the command line interface only with the aws command and put the object directly into the command, then it works.. but the powershell does not.

like image 525
Khan Avatar asked May 24 '26 07:05

Khan


1 Answers

  • You're passing a JSON string to an external program (aws)

  • Irrespective of how you constructed that string - directly as a string, or from an object / hashtable via ConvertTo-Json - as of PowerShell 7.1 - manual escaping of embedded " as \" is required, which, in turn requires escaping the preexisting \ preceding the embedded " as \\.

    • Note: None of this should be necessary, but due to a long-standing bug in PowerShell is - see this answer for details.

    • PowerShell Core 7.2.0-preview.5 now includes experimental feature PSNativeCommandArgumentPassing with an attempted fix, but, unfortunately, it looks like it will lack important accommodations for high-profile CLIs on Windows - see this summary from GitHub issue #15143. However, it would fix calls to aws.exe, the Amazon Web Services CLI.

# The JSON string to be passed as-is.
$json = @'
[
  {
     "Sensor":"{\"id\":\"880/2021-04-13\",\"attributes\":\"green\",\"Name\":\"SensorGreen\",\"state\":\"SUCCEEDED\"}",
     "Source":"google.com"
  }

]
'@

# Up to at least PowerShell 7.1:
# Manually perform the required escaping, to work around PowerShell's
# broken argument-passing to external programs.
# Note:
#  -replace '\\', '\\' *looks* like a no-op, but replaces each '\' with '\\'
$jsonEscaped = $json -replace '\\', '\\' -replace '"', '\"'

# Now pass the *escaped* JSON string to the aws CLI:
aws events put-events --entries $jsonEscaped --region "eu-central-1"  
like image 167
mklement0 Avatar answered May 26 '26 18:05

mklement0



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!