Help creating JSON file

Hi,
I’m kinda new to JSON but really starting to get it’s value for structured textual data.
So I need to create a single record JSON in the below format.
I figure I need to populate an array, perhaps a sub array then converttojson???
Can’t find any clear guidelines to create the logical structure that i’m after.
Any ideas:

{
  "Person": {
    "Name": "Bill Gates",
    "Age": "65",
    "Gender": "Male",
    "Height": "177"
    "FormerGroups": {
      "Microsoft",
      "B&M Trust",
      "Readers Digest",
      "Covid Task Force",
    }
}

Many thanks.

I’m not sure if I really got the question. Usually you wouldn’t create it by hand. But if you wanted to you could use a combination of hash tables and arrays … something like this:

$SourceHash = @{
    Person = @{
        Name         = 'Bill Gates';
        Gender       = 'Male';
        Height       = 177;
        Age          = 65;
        FormerGroups = @(
            'Covid Task Force',
            'Readers Digest',
            'Microsoft',
            'B&M Trust'
        )
    }
} 
$JSON = $SourceHash | ConvertTo-Json
$JSON
2 Likes

Just to add to @Olaf’s answer. If the order is important, you can use the [ordered] type accelerator to preserve the structure:

$SourceHash = [ordered]@{
    Person = [ordered]@{
        Name         = 'Bill Gates';
        Gender       = 'Male';
        Height       = 177;
        Age          = 65;
        FormerGroups = @(
            'Covid Task Force',
            'Readers Digest',
            'Microsoft',
            'B&M Trust'
        )
    }
} 
2 Likes

@Olaf, I’m confused by 'create it by hand;: do you mean that you would usually just order some strings/variables in your code into JSON output?

In any case, I appreciate your clear example.

I meant if it’s not about some configuration data for a script you wrote yourself the data would probably come from an app, external program or a webservice or an API or something like this.

Yeah, I was illustrating what I would see the output looking like.
How do I convert these, into the JSON?

$Person = @{"Name":"Bill Gates";"Age":"65";"Gender":"Male";"Height":"177";}
$FormerGroups = @{"Microsoft";"B&M Trust";"Readers Digest";"CovidTaskForce";}

Neither of those are correctly formatted. Assuming $Person is a hashtable (should be =, not : ) and $FormerGroups is an array (should be a , and not a ; ), then you could do something like this:

$Person = @{"Name"="Bill Gates";"Age"="65";"Gender"="Male";"Height"="177";}
$FormerGroups = @("Microsoft","B&M Trust","Readers Digest","CovidTaskForce")
$person.Add('FormerGroups',$FormerGroups)

[PSCustomObject]$person | ConvertTo-Json
1 Like

Hmmmm … what’s the actual question? I assume it’s not about converting some funny values you pieced together by hand, isn’t it? What is it about?

Thanks @rob-simmers apologies I’d messed the syntax up accidentally but you got my drift. I just needed a steer how to programmatically create the JSON from array and hash data.
Easy when you know how :slightly_smiling_face: