# Help creating JSON file

**URL:** <https://forums.powershell.org/t/help-creating-json-file/16488>\
**Category:** PowerShell Help\
**Created:** [May 11, 2021, 4:27pm UTC](https://forums.powershell.org/t/help-creating-json-file/16488 "2021-05-11T16:27:01Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![dawsonweb](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/dawsonweb/32/22_2.png) [@dawsonweb](https://forums.powershell.org/u/dawsonweb)\
**Post date:** [May 11, 2021, 4:27pm UTC](https://forums.powershell.org/t/help-creating-json-file/16488/1 "2021-05-11T16:27:01Z")

</div>

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:

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

```

Many thanks.

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [May 11, 2021, 5:13pm UTC](https://forums.powershell.org/t/help-creating-json-file/16488/3 "2021-05-11T17:13:45Z")

</div>

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:

```auto
$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

```

---

<div class="post-metadata">

**Author:** ![matt-bloomfield](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@matt-bloomfield](https://forums.powershell.org/u/matt-bloomfield)\
**Post date:** [May 11, 2021, 5:50pm UTC](https://forums.powershell.org/t/help-creating-json-file/16488/4 "2021-05-11T17:50:13Z")

</div>

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

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

```

---

<div class="post-metadata">

**Author:** ![ShaunBlake](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/shaunblake/32/1273_2.png) [@ShaunBlake](https://forums.powershell.org/u/ShaunBlake)\
**Post date:** [May 11, 2021, 6:21pm UTC](https://forums.powershell.org/t/help-creating-json-file/16488/5 "2021-05-11T18:21:57Z")

</div>

> [@Olaf](#):
>
> Usually you wouldn’t create it by hand. But…

@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.

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [May 11, 2021, 7:10pm UTC](https://forums.powershell.org/t/help-creating-json-file/16488/6 "2021-05-11T19:10:35Z")

</div>

> [@ShaunBlake](#):
>
> do you mean that you would usually just order some strings/variables in your code into JSON output?

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.

---

<div class="post-metadata">

**Author:** ![dawsonweb](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/dawsonweb/32/22_2.png) [@dawsonweb](https://forums.powershell.org/u/dawsonweb)\
**Post date:** [May 11, 2021, 7:23pm UTC](https://forums.powershell.org/t/help-creating-json-file/16488/7 "2021-05-11T19:23:23Z")

</div>

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

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

```

---

<div class="post-metadata">

**Author:** ![rob-simmers](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rob-simmers/32/1010_2.png) [@rob-simmers](https://forums.powershell.org/u/rob-simmers)\
**Post date:** [May 11, 2021, 8:17pm UTC](https://forums.powershell.org/t/help-creating-json-file/16488/8 "2021-05-11T20:17:39Z")

</div>

> [@dawsonweb](#):
>
> ```auto
> $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:

```auto
$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

```

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [May 11, 2021, 9:02pm UTC](https://forums.powershell.org/t/help-creating-json-file/16488/9 "2021-05-11T21:02:33Z")

</div>

> [@dawsonweb](#):
>
> Yeah, I was illustrating what I would see the output looking like.  
> How do I convert these, into the JSON?

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?

---

<div class="post-metadata">

**Author:** ![dawsonweb](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/dawsonweb/32/22_2.png) [@dawsonweb](https://forums.powershell.org/u/dawsonweb)\
**Post date:** [May 11, 2021, 10:34pm UTC](https://forums.powershell.org/t/help-creating-json-file/16488/10 "2021-05-11T22:34:19Z")

</div>

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 🙂

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:26pm UTC](https://forums.powershell.org/t/help-creating-json-file/16488/11 "2024-05-16T20:26:26Z")

</div>

Closed on May 16, 2024, 8:26pm UTC


