Welcome › Forums › General PowerShell Q&A › Generate JSON from an JSON export
- This topic has 1 reply, 2 voices, and was last updated 4 months, 4 weeks ago by
Participant.
Viewing 1 reply thread
-
AuthorPosts
-
-
August 20, 2020 at 6:20 am #251231
Hi all,
I’ve got this json file exported from an application :
{"Id": "rslvr-rr-ea6e4ec113014e1fb","CreatorRequestId": "615a6b4d-bd7f-4c3e-9b7b-fe8f6302c9bb","Status": "COMPLETE","Name":"myName1","OwnerId": "704448923565","ShareStatus": "SHARED_WITH_ME"},{"Id": "rslvr-rr-f822f0e1e9bc4ec2a","CreatorRequestId": "f2a55287-a009-4576-a165-2c0cbbec6d46","Status": "COMPLETE","Name": "myName2","OwnerId": "704448923565","ShareStatus": "SHARED_WITH_ME"},{"Id": "rslvr-rr-cdc38091a5984da3a","CreatorRequestId": "b93dba57-200b-4c96-94f4-757ec69a7bf0","Status": "COMPLETE","Name": "myName3","OwnerId": "704448923565","ShareStatus": "SHARED_WITH_ME"}}This is a bit part of my big json file, what I would like to do is reading this JSON (I know how to do that) and create an second JSON from this first source. And this is where I’ve got issue.Here is my code :$myRules = get-content .\output.json | ConvertFrom-Json | select -expand Rules
foreach($Rules in $myRules ) {
$Json= @{
Resources = @{
$Rules.Id = @{
Type= "this is a type of ressource"
Properties = @(
@{
Name=$Rules.Name
RuleId = $Rules.Id
FinalDestination = "This is my Destination"
}
)
}
}
}
}
$Json |ConvertTo-Json -Depth 4What I want is a json like that :
"Resources": {"myName1": {"Type": "this is a type of ressource","Properties": {"Name": "myName1","RuleId": "rslvr-rr-ea6e4ec113014e1fb","FinalDestination": "This is my Destination"},"myName2": {"Type": "this is a type of ressource","Properties": {"Name": "myName2","RuleId": "rslvr-rr-f822f0e1e9bc4ec2a","FinalDestination": "This is my Destination"},"myName3": {"Type": "this is a type of ressource","Properties": {"Name": "myName3","RuleId": "rslvr-rr-cdc38091a5984da3a","FinalDestination": "This is my Destination"}}}Here is my actual result..:{
"Resources": {
"myName3": {
"Properties": [
{
"FinalDestination": "This is my Destination",
"Name": "myName3",
"RuleId": "rslvr-rr-cdc38091a5984da3a"
}
],
"Type": "this is a type of ressource"
}
}
}Why the “type” is generated at the end?Regards-
This topic was modified 5 months, 1 week ago by
Fredoxmm97.
-
This topic was modified 5 months, 1 week ago by
-
August 31, 2020 at 8:22 pm #253619
Consider the following:
PowerShell12345678910111213141516171819$Json = [ordered]@{ Resources = @() }foreach($Rules in $myRules ){$Json.Resources +=[ordered]@{$Rules.Name = [ordered]@{Type = "this is a type of resource"Properties = [ordered]@{Name = $Rules.NameRuleId = $Rules.IdFinalDestination = "This is my Destination"}}}}$Json | ConvertTo-Json -Depth 4Here was my output (which is JSON compliant):
JavaScript12345678910111213141516171819202122232425262728293031323334{"Resources": [{"myName1": {"Type": "this is a type of resource","Properties": {"Name": "myName1","RuleId": "rslvr-rr-ea6e4ec113014e1fb","FinalDestination": "This is my Destination"}}},{"myName2": {"Type": "this is a type of resource","Properties": {"Name": "myName2","RuleId": "rslvr-rr-f822f0e1e9bc4ec2a","FinalDestination": "This is my Destination"}}},{"myName3": {"Type": "this is a type of resource","Properties": {"Name": "myName3","RuleId": "rslvr-rr-cdc38091a5984da3a","FinalDestination": "This is my Destination"}}}]}Note:
- Use of array in the expected spot
- Use of [ordered] to force as-is property sequencing
- Use of += to append rather than overwrite the $Json variable
-
-
AuthorPosts
Viewing 1 reply thread
- The topic ‘Generate JSON from an JSON export’ is closed to new replies.