# Add variables in JSON file into powershell

**URL:** <https://forums.powershell.org/t/add-variables-in-json-file-into-powershell/17351>\
**Category:** PowerShell Help\
**Created:** [August 19, 2021, 6:28pm UTC](https://forums.powershell.org/t/add-variables-in-json-file-into-powershell/17351 "2021-08-19T18:28:58Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![aman1313](https://avatars.discourse-cdn.com/v4/letter/a/b487fb/32.png) [@aman1313](https://forums.powershell.org/u/aman1313)\
**Post date:** [August 19, 2021, 6:28pm UTC](https://forums.powershell.org/t/add-variables-in-json-file-into-powershell/17351/1 "2021-08-19T18:28:58Z")

</div>

Hi,

I am trying to add variable in JSON file for using that file into Invoke-RestMethod command. But while running this here string ($body) I am not able to extract values out of these variables $Name and $SerialNumber. Below is my code. Please help.

$Body = @"  
{  
“hardware”: {  
“name”: $Name,  
“bio”: {  
“manufacturer”: “Lenovo”,  
“ssn”: $SerialNumber,  
“model”: “T470”,  
}  
}  
}  
"@

Thanks in advance

---

<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:** [August 19, 2021, 6:54pm UTC](https://forums.powershell.org/t/add-variables-in-json-file-into-powershell/17351/2 "2021-08-19T18:54:08Z")

</div>

I’m not sure if I understood what you’re trying to do. If you want to create a JSON object it might be easier to create it from a PowerShell object (hash table) instead of a here string.

```auto
$Body = @{
    hardware = @{
        name = $Name
        bio = @{
            manufacturer = 'Lenovo'
            ssn = $SerialNumber
            model = 'T470'
        }
    }
}

$Body | ConvertTo-Json

```

And BTW: Please format your code as code. Thanks. 😉

---

<div class="post-metadata">

**Author:** ![aman1313](https://avatars.discourse-cdn.com/v4/letter/a/b487fb/32.png) [@aman1313](https://forums.powershell.org/u/aman1313)\
**Post date:** [August 19, 2021, 7:33pm UTC](https://forums.powershell.org/t/add-variables-in-json-file-into-powershell/17351/3 "2021-08-19T19:33:18Z")

</div>

Actually the code I posted was a payload in JSON format that I will use into

Invoke-RestMethod -Method PUT -Headers $header -Uri $URI2 -Body $Body

Let me see If your solution works for me

---

<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:** [August 19, 2021, 7:38pm UTC](https://forums.powershell.org/t/add-variables-in-json-file-into-powershell/17351/4 "2021-08-19T19:38:49Z")

</div>

> [@aman1313](#):
>
> Actually the code I posted was a payload in JSON format that I will use into

Did you create it _by hand_? If the answer is “yes” - why? 😉

Please format your code as code. Thanks.

---

<div class="post-metadata">

**Author:** ![aman1313](https://avatars.discourse-cdn.com/v4/letter/a/b487fb/32.png) [@aman1313](https://forums.powershell.org/u/aman1313)\
**Post date:** [August 19, 2021, 8:05pm UTC](https://forums.powershell.org/t/add-variables-in-json-file-into-powershell/17351/5 "2021-08-19T20:05:11Z")

</div>

Actually I download full payload from online then there were many attributes that I do not need, so I delete them.

---

<div class="post-metadata">

**Author:** ![aman1313](https://avatars.discourse-cdn.com/v4/letter/a/b487fb/32.png) [@aman1313](https://forums.powershell.org/u/aman1313)\
**Post date:** [August 19, 2021, 9:19pm UTC](https://forums.powershell.org/t/add-variables-in-json-file-into-powershell/17351/6 "2021-08-19T21:19:37Z")

</div>

Basically my question is, Is it possible to store variable in a payload (JSON) in powershell so that I can enumerate values in that variable by using foreach from CSV?

---

<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:** [August 19, 2021, 10:21pm UTC](https://forums.powershell.org/t/add-variables-in-json-file-into-powershell/17351/7 "2021-08-19T22:21:44Z")

</div>

I am - again - not sure if I understand. You can do whatever you need to do. But if I got you right I think it would be easier to work with proper objects because PowerShell is made for. And when you need this objects to be converted to JSON you can do whenever you need.

CSV files or CSV data are best for structured data. The data you seem to be dealing with here are obviously hierarchical data. You could save them for later use with Export-CliXML or as strings in JSON or XML. But it will be easier to convert them before manipulating them.

---

<div class="post-metadata">

**Author:** ![aman1313](https://avatars.discourse-cdn.com/v4/letter/a/b487fb/32.png) [@aman1313](https://forums.powershell.org/u/aman1313)\
**Post date:** [August 20, 2021, 4:20pm UTC](https://forums.powershell.org/t/add-variables-in-json-file-into-powershell/17351/8 "2021-08-20T16:20:36Z")

</div>

Thank You so much @Olaf

It worked for me, so I have used hash table as you suggested, convert to JSON and used final output in Invoke-Restmethod.

---

<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:** [August 20, 2021, 4:46pm UTC](https://forums.powershell.org/t/add-variables-in-json-file-into-powershell/17351/9 "2021-08-20T16:46:47Z")

</div>

Great. 👍🏽 I’m glad it helped. 🙂

---

<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:21pm UTC](https://forums.powershell.org/t/add-variables-in-json-file-into-powershell/17351/10 "2024-05-16T20:21:58Z")

</div>


