Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
137 views
in Technique[技术] by (71.8m points)

How to convert a standard json file to aws sqs cli json input file format for powershell

I want to send a standard formatted json file via a powershell CLI commend line to a aws sqs normal queue.

The following command works fine

PS> aws sqs send-message --queue-url https://sqs.eu-central-1.amazonaws.com/xxxx/Test --message-body "Data delivery" --message-attribute file://send-message10.json

The file send-message10.json has the following format:

{
  "City": {
    "DataType": "String",
    "StringValue": "Any City"
  },
  "Population": {
    "DataType": "Number",
    "StringValue": "1250800"
  }
}

Now I want to transfer a standard formatted json data.json to the same queue The file data.json has the following format:

[
  {
    "DateandTime": "2021-01-14T16:30:31.000Z",
    "Value1": 3,
    "Value2": 13939.71
  }
]

How can I easily either a) modify the command line so that my data.json file is accepted or b) convert the data.json file into the send-message10.json format to be accepted by the command line?

question from:https://stackoverflow.com/questions/65835253/how-to-convert-a-standard-json-file-to-aws-sqs-cli-json-input-file-format-for-po

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Your data.json is an array with a single name-value pair.. so, convert it to a PSCustomObject, reference the first element in the array, and convert it back to JSON.

(@'
[
  {
    "DateandTime": "2021-01-14T16:30:31.000Z",
    "Value1": 3,
    "Value2": 13939.71
  }
]
'@ | ConvertFrom-Json)[0] | ConvertTo-Json

results in:

{
    "DateandTime":  "2021-01-14T16:30:31.000Z",
    "Value1":  3,
    "Value2":  13939.71
}

You can probably add more logic on the command line, but I hope it helps.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...