Flatting nested .json-files that start with "[" when | ConvertFrom-Json gives the wrong output

119 views Asked by At

I am trying to get a format of the .json file that gives a correct output when using | ConvertFrom-Json. I tend to loose a lot of information, and the returned object does not work as an input to the desired function to flatten the .json file.

I want to get a correct output from ConvertFrom-Json so that it works with the script: Convert-JsonToCsv.ps1 from this GitHub page.

I have the following .json file

[
    {
        "[email protected]":  "https://graph.microsoft.com/beta/deviceManagement/groupPolicyDefinitions(\u0027670ed8e1-25f4-4c24-b9ea-04b861a4566f\u0027)",
        "enabled":  false
    },
    {
        "[email protected]":  "https://graph.microsoft.com/beta/deviceManagement/groupPolicyDefinitions(\u00273705e5ff-18e7-4051-9d6e-12cc93eca35b\u0027)",
        "presentationValues":  [
                                   {
                                       "@odata.type":  "#microsoft.graph.groupPolicyPresentationValueList",
                                       "values":  [
                                                      {
                                                          "name":  "*",
                                                          "value":  null
                                                      }
                                                  ],
                                       "[email protected]":  "https://graph.microsoft.com/beta/deviceManagement/groupPolicyDefinitions(\u00273705e5ff-18e7-4051-9d6e-12cc93eca35b\u0027)/presentations(\u002715374e33-8b97-4d41-8253-9b90ce9bc645\u0027)"
                                   }
                               ],
        "enabled":  true
    },
    {
        "[email protected]":  "https://graph.microsoft.com/beta/deviceManagement/groupPolicyDefinitions(\u0027c12cd0b7-a46f-4cc5-8dbd-1ce17d69c702\u0027)",
        "enabled":  false
    },
    {
        "[email protected]":  "https://graph.microsoft.com/beta/deviceManagement/groupPolicyDefinitions(\u002763db8dde-bcaf-49de-8964-1f68371c4d2c\u0027)",
        "enabled":  false
    },
    {
        "[email protected]":  "https://graph.microsoft.com/beta/deviceManagement/groupPolicyDefinitions(\u0027b0689b0a-b725-4070-a5bf-32be09e495cf\u0027)",
        "enabled":  false
    },
    {
        "[email protected]":  "https://graph.microsoft.com/beta/deviceManagement/groupPolicyDefinitions(\u002784c82f32-04f7-4074-a5c4-3544a98208b8\u0027)",
        "enabled":  true
    },
    {
        "[email protected]":  "https://graph.microsoft.com/beta/deviceManagement/groupPolicyDefinitions(\u002708c228ef-25e3-45b2-9afe-47bb2f2ab7eb\u0027)",
        "presentationValues":  [
                                   {
                                       "@odata.type":  "#microsoft.graph.groupPolicyPresentationValueText",
                                       "value":  "2",
                                       "[email protected]":  "https://graph.microsoft.com/beta/deviceManagement/groupPolicyDefinitions(\u002708c228ef-25e3-45b2-9afe-47bb2f2ab7eb\u0027)/presentations(\u0027cb2eb670-db0b-4efc-97b3-36b7e13e576e\u0027)"
                                   }
                               ],
        "enabled":  true
    },
    {
        "[email protected]":  "https://graph.microsoft.com/beta/deviceManagement/groupPolicyDefinitions(\u00279777a6fc-7311-4842-a2ae-5b34e4c6b94a\u0027)",
        "presentationValues":  [
                                   {
                                       "@odata.type":  "#microsoft.graph.groupPolicyPresentationValueList",
                                       "values":  [
                                                      {
                                                          "name":  "[*.]didac.no",
                                                          "value":  null
                                                      },
                                                      {
                                                          "name":  "clikc.wcoomd.org",
                                                          "value":  null
                                                      },
                                                      {
                                                          "name":  "norsktollerforbund.sharepoint.com",
                                                          "value":  null
                                                      },
                                                      {
                                                          "name":  "https://p360pd.toll.no",
                                                          "value":  null
                                                      },
                                                      {
                                                          "name":  "https://regnskap-agrdra.dfo.no",
                                                          "value":  null
                                                      }
                                                  ],
                                       "[email protected]":  "https://graph.microsoft.com/beta/deviceManagement/groupPolicyDefinitions(\u00279777a6fc-7311-4842-a2ae-5b34e4c6b94a\u0027)/presentations(\u0027cf6985ea-33cb-4bb8-bf10-02c1aa49f35f\u0027)"
                                   }
                               ],
        "enabled":  true
    },
    {
        "[email protected]":  "https://graph.microsoft.com/beta/deviceManagement/groupPolicyDefinitions(\u00273e3ef429-ba9c-4bf5-9c54-5caf3aac9831\u0027)",
        "presentationValues":  [
                                   {
                                       "@odata.type":  "#microsoft.graph.groupPolicyPresentationValueText",
                                       "value":  "https://www.google.com",
                                       "[email protected]":  "https://graph.microsoft.com/beta/deviceManagement/groupPolicyDefinitions(\u00273e3ef429-ba9c-4bf5-9c54-5caf3aac9831\u0027)/presentations(\u0027bb6876d7-90c5-4281-a9a2-7154ed9afd79\u0027)"
                                   }
                               ],
        "enabled":  true
    },
    {
        "[email protected]":  "https://graph.microsoft.com/beta/deviceManagement/groupPolicyDefinitions(\u002746436297-0d1e-45db-be13-5e3fccff84af\u0027)",
        "presentationValues":  [
                                   {
                                       "@odata.type":  "#microsoft.graph.groupPolicyPresentationValueText",
                                       "value":  "2",
                                       "[email protected]":  "https://graph.microsoft.com/beta/deviceManagement/groupPolicyDefinitions(\u002746436297-0d1e-45db-be13-5e3fccff84af\u0027)/presentations(\u0027d00346f1-de43-4797-bf2e-fbe1db8acbc6\u0027)"
                                   }
                               ],
        "enabled":  true
    },
    {
        "[email protected]":  "https://graph.microsoft.com/beta/deviceManagement/groupPolicyDefinitions(\u00277d66e7bd-0a01-4f66-8d76-690a58276985\u0027)",
        "enabled":  false
    },
    {
        "[email protected]":  "https://graph.microsoft.com/beta/deviceManagement/groupPolicyDefinitions(\u0027df68228c-1238-41da-975a-71ce2b241b12\u0027)",
        "presentationValues":  [
                                   {
                                       "@odata.type":  "#microsoft.graph.groupPolicyPresentationValueText",
                                       "value":  "2",
                                       "[email protected]":  "https://graph.microsoft.com/beta/deviceManagement/groupPolicyDefinitions(\u0027df68228c-1238-41da-975a-71ce2b241b12\u0027)/presentations(\u0027a0c5718b-5463-42a1-8888-18d2dea6622c\u0027)"
                                   }
                               ],
        "enabled":  true
    },
    {
        "[email protected]":  "https://graph.microsoft.com/beta/deviceManagement/groupPolicyDefinitions(\u0027bcbc275c-0c2e-41ee-826e-77a0c8ef3dfc\u0027)",
        "enabled":  true
    },
    {
        "[email protected]":  "https://graph.microsoft.com/beta/deviceManagement/groupPolicyDefinitions(\u0027732b39c3-e2c5-4c6c-a6a3-9b4fcdde3feb\u0027)",
        "enabled":  true
    },
    {
        "[email protected]":  "https://graph.microsoft.com/beta/deviceManagement/groupPolicyDefinitions(\u002732cb2ceb-a08a-4406-8977-a1a124c2de42\u0027)",
        "presentationValues":  [
                                   {
                                       "@odata.type":  "#microsoft.graph.groupPolicyPresentationValueList",
                                       "values":  [
                                                      {
                                                          "name":  "com.microsoft.browsercore",
                                                          "value":  null
                                                      }
                                                  ],
                                       "[email protected]":  "https://graph.microsoft.com/beta/deviceManagement/groupPolicyDefinitions(\u002732cb2ceb-a08a-4406-8977-a1a124c2de42\u0027)/presentations(\u0027f849b343-4761-4326-ae50-9dc1eeb87383\u0027)"
                                   }
                               ],
        "enabled":  true
    },
    {
        "[email protected]":  "https://graph.microsoft.com/beta/deviceManagement/groupPolicyDefinitions(\u00270a125ca8-f0eb-4914-83ee-a41ee85a0e2b\u0027)",
        "enabled":  true
    },
    {
        "[email protected]":  "https://graph.microsoft.com/beta/deviceManagement/groupPolicyDefinitions(\u0027c4282b30-71ac-4c45-b4e3-b317211fd5e8\u0027)",
        "presentationValues":  [
                                   {
                                       "@odata.type":  "#microsoft.graph.groupPolicyPresentationValueText",
                                       "value":  "0",
                                       "[email protected]":  "https://graph.microsoft.com/beta/deviceManagement/groupPolicyDefinitions(\u0027c4282b30-71ac-4c45-b4e3-b317211fd5e8\u0027)/presentations(\u00274f5c7fa8-a533-48e5-a0ba-bc4f5e9dbb16\u0027)"
                                   }
                               ],
        "enabled":  true
    },
    {
        "[email protected]":  "https://graph.microsoft.com/beta/deviceManagement/groupPolicyDefinitions(\u00271c484cb4-c95c-45ee-aae6-cfaafcae6d61\u0027)",
        "enabled":  false
    },
    {
        "[email protected]":  "https://graph.microsoft.com/beta/deviceManagement/groupPolicyDefinitions(\u0027124c9a03-7378-44b6-914b-e00f63dd399b\u0027)",
        "enabled":  true
    },
    {
        "[email protected]":  "https://graph.microsoft.com/beta/deviceManagement/groupPolicyDefinitions(\u002709d328bc-a4a6-414d-abe3-ed453ca82172\u0027)",
        "enabled":  true
    },
    {
        "[email protected]":  "https://graph.microsoft.com/beta/deviceManagement/groupPolicyDefinitions(\u0027fb18f15f-d16e-45f9-a79c-efa4da12203f\u0027)",
        "presentationValues":  [
                                   {
                                       "@odata.type":  "#microsoft.graph.groupPolicyPresentationValueText",
                                       "value":  "2",
                                       "[email protected]":  "https://graph.microsoft.com/beta/deviceManagement/groupPolicyDefinitions(\u0027fb18f15f-d16e-45f9-a79c-efa4da12203f\u0027)/presentations(\u0027e3bb6241-cdd2-4dec-a276-e6898a2725bd\u0027)"
                                   }
                               ],
        "enabled":  true
    },
    {
        "[email protected]":  "https://graph.microsoft.com/beta/deviceManagement/groupPolicyDefinitions(\u0027d4e64503-0338-4cae-9712-efec0461b8fe\u0027)",
        "presentationValues":  [
                                   {
                                       "@odata.type":  "#microsoft.graph.groupPolicyPresentationValueText",
                                       "value":  "4",
                                       "[email protected]":  "https://graph.microsoft.com/beta/deviceManagement/groupPolicyDefinitions(\u0027d4e64503-0338-4cae-9712-efec0461b8fe\u0027)/presentations(\u0027c67624ce-2404-4740-a7d1-da572d847231\u0027)"
                                   }
                               ],
        "enabled":  true
    }
]

What I have tried has worked with other .json files, but not the ones that start with "[" and end with "]". The following:

$input = Get-Content -Path $jsonFileContents | ConvertFrom-Json

Gives the output (named $input):

[email protected]                                                                                            enabled
---------------------                                                                                            -------
https://graph.microsoft.com/beta/deviceManagement/groupPolicyDefinitions('670ed8e1-25f4-4c24-b9ea-04b861a4566f')   False
https://graph.microsoft.com/beta/deviceManagement/groupPolicyDefinitions('3705e5ff-18e7-4051-9d6e-12cc93eca35b')    True
https://graph.microsoft.com/beta/deviceManagement/groupPolicyDefinitions('c12cd0b7-a46f-4cc5-8dbd-1ce17d69c702')   False
https://graph.microsoft.com/beta/deviceManagement/groupPolicyDefinitions('63db8dde-bcaf-49de-8964-1f68371c4d2c')   False
https://graph.microsoft.com/beta/deviceManagement/groupPolicyDefinitions('b0689b0a-b725-4070-a5bf-32be09e495cf')   False
https://graph.microsoft.com/beta/deviceManagement/groupPolicyDefinitions('84c82f32-04f7-4074-a5c4-3544a98208b8')    True
https://graph.microsoft.com/beta/deviceManagement/groupPolicyDefinitions('08c228ef-25e3-45b2-9afe-47bb2f2ab7eb')    True
https://graph.microsoft.com/beta/deviceManagement/groupPolicyDefinitions('9777a6fc-7311-4842-a2ae-5b34e4c6b94a')    True
https://graph.microsoft.com/beta/deviceManagement/groupPolicyDefinitions('3e3ef429-ba9c-4bf5-9c54-5caf3aac9831')    True
https://graph.microsoft.com/beta/deviceManagement/groupPolicyDefinitions('46436297-0d1e-45db-be13-5e3fccff84af')    True
https://graph.microsoft.com/beta/deviceManagement/groupPolicyDefinitions('7d66e7bd-0a01-4f66-8d76-690a58276985')   False
https://graph.microsoft.com/beta/deviceManagement/groupPolicyDefinitions('df68228c-1238-41da-975a-71ce2b241b12')    True
https://graph.microsoft.com/beta/deviceManagement/groupPolicyDefinitions('bcbc275c-0c2e-41ee-826e-77a0c8ef3dfc')    True
https://graph.microsoft.com/beta/deviceManagement/groupPolicyDefinitions('732b39c3-e2c5-4c6c-a6a3-9b4fcdde3feb')    True
https://graph.microsoft.com/beta/deviceManagement/groupPolicyDefinitions('32cb2ceb-a08a-4406-8977-a1a124c2de42')    True
https://graph.microsoft.com/beta/deviceManagement/groupPolicyDefinitions('0a125ca8-f0eb-4914-83ee-a41ee85a0e2b')    True
https://graph.microsoft.com/beta/deviceManagement/groupPolicyDefinitions('c4282b30-71ac-4c45-b4e3-b317211fd5e8')    True
https://graph.microsoft.com/beta/deviceManagement/groupPolicyDefinitions('1c484cb4-c95c-45ee-aae6-cfaafcae6d61')   False
https://graph.microsoft.com/beta/deviceManagement/groupPolicyDefinitions('124c9a03-7378-44b6-914b-e00f63dd399b')    True
https://graph.microsoft.com/beta/deviceManagement/groupPolicyDefinitions('09d328bc-a4a6-414d-abe3-ed453ca82172')    True
https://graph.microsoft.com/beta/deviceManagement/groupPolicyDefinitions('fb18f15f-d16e-45f9-a79c-efa4da12203f')    True
https://graph.microsoft.com/beta/deviceManagement/groupPolicyDefinitions('d4e64503-0338-4cae-9712-efec0461b8fe')    True

Thus a lot of information is lost.

Using other types of files (starting and ending with { .. }) I haev used the following, where Flatten-PsCustomObject is from the git-link above

$input = Get-Content -Path $inputFile | ConvertFrom-Json 
Flatten-PsCustomObject 'someString' $input 

This works perfectly. What am I doing wrong / how do I handle the [ ... ]-files? Thank you for your input on the matter!

1

There are 1 answers

0
js2010 On

This is a common question. Pipe it to format-list to see the different sets of properties. Sometimes you have to specify "convertfrom-json -depth" greater than 2. The problem isn't exactly json arrays. But the default output (format-table) won't show a varying number of properties between objects in an array.

get-content file.json | convertfrom-json | format-list