1

I am trying to update JSON file with a single value. The file is updated but is getting updated with a bunch of extra data that I don't want or need.

Here is a snippet from the PowerShell script:

Id.txt contains one string "16963e76"

$outPath = "C:\Program Files (x86)\Go Agent\pipelines\mypipeline\Application\Application\Id.txt"
$imageid = Get-Content $outPath
$filejson = Get-Content -Raw -Path $file.FullName -Encoding UTF8 | ConvertFrom-Json
$filejson.parameters.Image = $imageid
$filejson | ConvertTo-Json | Out-File $filepath -Encoding utf8 -Force

Here is what I expect my JSON to look like:

{
  "name": "perf-a",
  "cft_file":"cft/cft.json",
  "parameters": {
    "AppEnvironmentType": "perf",
    "Image": "16963e76"
  },
  "tags": {
    "Owner": "[email protected]",
    "CostCenter": "12345"
  }
}

Here is what my JSON actually looks like after the update:

{
    "name":  "perf-a",
    "cft_file":  "cft/cft.json",
    "parameters":  {
        "AppEnvironmentType":  "perf",
        "Image":  {
            "value":  "16963e76",
            "PSPath":  "C:\\Program Files (x86)\\Go Agent\\pipelines\\mypipeline\\Application\\Application\\Id.txt",
            "PSParentPath":  "C:\\Program Files (x86)\\Go Agent\\pipelines\\mypipeline\\Application\\Application",
            "PSChildName":  "Id.txt",
            "PSDrive":  "C",
            "PSProvider":  "Microsoft.PowerShell.Core\\FileSystem",
            "ReadCount":  1
        }
    },
    "tags":  {
        "Owner":  "[email protected]",
        "CostCenter":  "12345"
    }
}

What is causing this issue and how can I stop it from happening?

Thanks,

Rhonda

1
  • 1
    OK so when you import the string from id.txt powershell is adding several noteproperties from the environment, usually these don't matter and can be ignored. However ConvertTo-JSON is extracting these note properties from the object and inserting them into your json. By using the tostring method my solution works by changing what was a string into another string, but this time without the note properties, so it works. Commented Dec 5, 2016 at 23:55

2 Answers 2

1

EDIT

added correct reason for solution working.

OK so when you import the string from id.txt powershell is adding several noteproperties from the environment, usually these don't matter and can be ignored.

However ConvertTo-JSON is extracting these note properties from the object and inserting them into your json. By using the tostring method my solution works by changing what was a string into another string, but this time without the note properties, so it works.

$outPath = "C:\Program Files (x86)\Go Agent\pipelines\mypipeline\Application\Application\Id.txt"
$imageid = (Get-Content $outPath).ToString()
$filejson = Get-Content -Raw -Path $file.FullName -Encoding UTF8 | ConvertFrom-Json
$filejson.parameters.Image = $imageid
$filejson | ConvertTo-Json | Out-File $filepath -Encoding utf8 -Force

PREVIOUS INCORRECT REASON:

The $imageid is actually an object when you import it like that and the convertto-json is putting the full object into your file.

You should make sure that $imageid is just a string and whilst there are a few ways of doing this you could use the tostring method as below.

Sign up to request clarification or add additional context in comments.

Comments

0

Probably change this:

`$filejson.parameters.Image = $imageid.value`

Because you are passing an object to the $filejson.parameters.Image, but you only need 1 property of that object.

2 Comments

$imageid is a [string] instance (due to having been assigned the output from Get-Content applied to a single-line input file), so it doesn't have a .value property (it is decorated with note properties added by PS, however, as Jim's answer explains).
P.S.: The value property in the JSON output in the question is an artifact of how ConvertTo-Json stringifies value types and strings that are decorated with NoteProperty and/or ScriptProperty members.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.