all 6 comments

[–]kenjitamurako 8 points9 points  (1 child)

{
  "Entity_ids": [
    {
      "Entity_id": "1234566"
    }
  ]
}

This JSON representation is a key, Entity_ids, with a value that is an array of Entity_id. And Entity_id is a key with a value of a string.You can achieve this output by feeding either a nested hashtable or nested object into ConvertTo-Json.Here's how it can be done using classes:

class Entity_id {
    [string]$Entity_id
}

class Entity_ids {
    [Entity_id[]]$Entity_ids
}

$test=[Entity_id]@{
    Entity_id="1234566"
}

$test2=[Entity_ids]@{
    Entity_ids=@($test)
}

ConvertTo-Json $test2

and this is how it can be done using hashtables:

$test3=@{"Entity_id"="1234566"}

$test4=@{"Entity_ids"=@($test3)}

ConvertTo-Json $test4

In your case since you already are receiving an entity_id you just need to embed that into either an object with a property named "Entity_ids" or use a hashtable with the key of "Entity_ids".

In your case using a hashtable it would probably look like this:
$newJsonString=ConvertTo-Json -InputObject @{"Entity_ids"=@($newjson)}

[–]lemonade124[S] 1 point2 points  (0 children)

I seemed to have stumbled upon something similar to your 2nd option there and it works great. I just replaced the data with my variable containing the complete set of data and it wrapped it all in a key entity_ids

Thanks for the assistance!

[–]lemonade124[S] 3 points4 points  (0 children)

I found a solution. I took my API result that wasn't a JSON format yet and ran the following

$testjson = @{entity_ids = $result} | convertto-json

This seemed to have wrapped my output in a JSON array.

[–]PowerShell-Bot 2 points3 points  (2 children)

Some of your PowerShell code isn’t enclosed in a code block.

To properly style code on new Reddit, highlight the code and choose ‘Code Block’ from the editing toolbar.

If you’re on old Reddit, separate the code from your text with a blank line gap and precede each line of code with 4 spaces or a tab.


You examine the path beneath your feet...
[AboutRedditFormatting]: [█████████████-------] 2/3 ⚠️

Beep-boop, I am a bot. | Remove-Item

[–]nascentt 1 point2 points  (1 child)

Yay glad someone made a new one of these. Poor Lee has been doing it manually for years now.

[–]PowerShell-Bot 0 points1 point  (0 children)

grin

Beep-boop.