0

I have an application in C# to display information from Active Directory using PowerShell.

class userps
{
    public string Name { get; set; }
    public bool Enabled { get; set; }
}

PSObject[] results = pipeline.Invoke().ToArray();
List<userps> listUserps = new List<userps>();
foreach (PSObject obj in results)
{
     lista = JsonConvert.DeserializeObject<List<userps>>(obj.ToString());
}

If the object returns data of at least two elements, for example:

[
  {
    "Name":"xxx",
    "Enabled":true
  },
  {
    "Name":"yyy",
    "Enabled":true
  }
]

Then everything is fine, List.Count = 2. If, however, it returns one element:

[
  {
    "Name":"xxx",
    "Enabled":true
  }
]

Then List.Count = 0 and there is an exception:

Newtonsoft.Json.JsonSerializationException: „Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[ConsoleApp1.userps]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object.
JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.
Path 'Name', line 2, position 11.”

How do I solve this problem so it would work for one element and also for several?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
MatDob
  • 3
  • 2
  • 1
    Inspect `obj.ToString()`. – CodeCaster Jun 11 '18 at 10:13
  • Possible duplicate of [JsonConvert.DeserializeObject>](https://stackoverflow.com/questions/44192365/jsonconvert-deserializeobjectienumerablebook) – SᴇM Jun 11 '18 at 10:14
  • Also: Possible duplicate of [Cannot deserialize the JSON array (e.g. 1,2,3) into type ' ' because type requires JSON object (e.g. {“name”:“value”}) to deserialize correctly](https://stackoverflow.com/questions/22557559/cannot-deserialize-the-json-array-e-g-1-2-3-into-type-because-type-requ) – SᴇM Jun 11 '18 at 10:17
  • 1
    @SeM your duplicated links are not related to this case. OP already knows and has a success when deserailizing an array. He only fails when the array contains **one element** – CodeNotFound Jun 11 '18 at 10:20
  • @CodeNotFound I just want to show OP that if he have correct json string and deserialized it correctly, it is unlikely to happen, cause it doesn't matter if there 1 element or 2. – SᴇM Jun 11 '18 at 11:35

1 Answers1

2

It looks like the object you get from your PowerShell command is not a collection and is serialized as an object in JSON. It's the common behaviour of PowerShell when a command returns only one object instead of a list.

Example:

Get-Service | ConvertTo-Json

[
    {
        "Name":  "AdobeFlashPlayerUpdateSvc",
        ...
    },  
    {
        "Name":  "ALG",
        ...
    },
    ...
]

Get-Service -Name 'NetLogon' | ConvertTo-Json

{
    "Name":  "NetLogon",
    ...
}

To avoid this, encapsulate your command in an array constructor and replace the pipe by the InputObject parameter:

ConvertTo-Json -InputObject @(Get-Service -Name 'NetLogon')

[
    {
        "Name":  "NetLogon",
        ...
    }
]
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Julien Nury
  • 297
  • 2
  • 14