how to remove tag @{} in power shell

Viewed 55

I want to displaying data from json to env file with this output

OnSubscription=True; Name=John; Surname=Smith 

but when I create data, its showing like this

@{OnSubscription=True; Name=John; Surname=Smith} 

and this is my script

$Path2 = ".\output\env"
$x = Get-Content .\basket.json| ConvertFrom-Json

foreach( $rootProperty in @($x.psobject.properties | where-object {$_.MemberType -eq "NoteProperty"}) )
{

    write-host "'$($rootProperty.Name)' = '$($rootProperty.Value)'"

    foreach( $childProperty in @($rootProperty.Value.psobject.properties | where-object {$_.MemberType -eq "NoteProperty"}) )
    {
        write-host "'$($childProperty.Name)' = '$($childProperty.Value)'"
         
        
    }

    $result = Add-Content -Path  $Path2 "$($rootProperty.Value) "

    $result = Add-Content -Path  $Path2 "$($childProperty.Value) " 

}

This is my sample of json

{
    "Customer":  {
                     "OnSubscription":  true,
                     "Name":  "John",
                     "Surname":  "Smith"
                 },
    "Data":  {
                 "Basket":  [
                                "apples",
                                "pears",
                                "oranges",
                                "strawberries"
                            ]
             }
}

How to remove this @{} ? I already follow this example Create .env file from a .json file but with power shell

1 Answers

Try this:

$json = @'
{
    "Customer": {
        "OnSubscription":  true,
        "Name":  "John",
        "Surname":  "Smith"
    },
    "Data": {
        "Basket": [
            "apples",
            "pears",
            "oranges",
            "strawberries"
        ]
    }
}
'@ | ConvertFrom-Json

$output = ($json.Customer.PSObject.Properties.Name | ForEach-Object {
    "$_={0}" -f $json.Customer.$_
}) -join '; '

$Path2 = ".\output\env"
$output | Out-File $path
  • From File:
$json = Get-Content .\basket.json -Raw | ConvertFrom-Json
$output = ($json.Customer.PSObject.Properties.Name | ForEach-Object {
    "$_={0}" -f $json.Customer.$_
}) -join '; '

$Path2 = ".\output\env"
$output | Out-File $path
Related