Generate JSON from an JSON export

Welcome Forums General PowerShell Q&A Generate JSON from an JSON export

Viewing 1 reply thread
  • Author
    Posts
    • #251231
      Participant
      Topics: 1
      Replies: 0
      Points: 11
      Rank: Member

      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 4

       

      What 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 1 month, 1 week ago by Fredoxmm97.
    • #253619
      Participant
      Topics: 0
      Replies: 1
      Points: 6
      Rank: Member

      Consider the following:

      Here was my output (which is JSON compliant):

      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

       

Viewing 1 reply thread
  • You must be logged in to reply to this topic.