0

I want to update the package.json file in my project. I'm doing this by using the ConvertFrom-JSON command of Powershell (version 7.2.5). But when it is converted from JSON to objects. The nested (complex) objects are empty. How can I prevent this?

This is the JSON file:

{
  "name": "demo",
  "displayName": "Demo",
  "version": "0.2.58",
  "releaseDate": "yesterday",
  "private": true,
  "scripts": {
    "start": "vue-cli-service serve",
    "serve": "vue-cli-service serve",
    "build": "vue-cli-service build",
    "lint": "vue-cli-service lint"
  },
  "dependencies": {
    "bootstrap": "^5.1.3",
    "core-js": "^3.6.5",
    "exifreader": "^3.14.1",
    "file-saver": "^2.0.5",
    "vue-flux": "^6.4.0",
    "vue-i18n": "^9.2.0-beta.23",
    "vue-router": "^4.0.0-0",
    "vuex": "^4.0.0-0"
  },
  "devDependencies": {
    "@types/file-saver": "^2.0.5",
    "@typescript-eslint/eslint-plugin": "^4.18.0",
    "@typescript-eslint/parser": "^4.18.0",
    "@vue/cli-plugin-babel": "~4.5.0",
    "@vue/cli-plugin-eslint": "~4.5.0",
    "@vue/cli-plugin-router": "~4.5.0",
    "@vue/cli-plugin-typescript": "~4.5.0",
    "@vue/cli-plugin-vuex": "~4.5.0",
    "@vue/cli-service": "~4.5.0",
    "@vue/compiler-sfc": "^3.0.0",
    "@vue/eslint-config-prettier": "^6.0.0",
    "@vue/eslint-config-typescript": "^7.0.0",
    "eslint": "^6.7.2",
    "eslint-plugin-prettier": "^3.3.1",
    "eslint-plugin-vue": "^7.0.0",
    "prettier": "^2.2.1",
    "sass": "^1.44.0",
    "sass-loader": "^10.2.0",
    "typescript": "~4.1.5"
  }
}

And this is the script I'm using:

$Path = "./package.json"

$Content = (Get-Content $Path ) | ConvertFrom-Json 
Write-Host $Content

$Content.releaseDate = "today"
Write-Host $Content

And when I run the script this is the result:

@{name=demo; displayName=Demo; version=0.2.58; releaseDate=yesterday; private=True; scripts=; dependencies=; devDependencies=}
@{name=demo; displayName=Demo; version=0.2.58; releaseDate=today; private=True; scripts=; dependencies=; devDependencies=}
Gert Rikkers
  • 21
  • 1
  • 5
  • 1
    Looks oke to me. What do you expect? `$Content |ConvertTo-Json`? – iRon Jul 27 '22 at 12:40
  • 1
    That is just the way PowerShell tries to print the object onto the console window. If you convert back to JSON and output that, you will see all data is present. – Theo Jul 27 '22 at 12:58
  • To add to Theo's comment: In general: `Write-Host` (which is only designed for writing to the _display_ and which cannot output _data_), performs space-separated, single-line output formatting based on simple `.ToString()` calls, which with complex objects typically results in unhelpful representations. For richly formatted display-only output, use `Out-Host` instead; for data output, use `Write-Output` or, better yet, PowerShell's _implicit_ output feature. Here, you can indeed pipe back to `ConvertTo-Json` or to `Format-Custom` for an informal representation. – mklement0 Jul 27 '22 at 13:13
  • iRon I was expecting object with filled properties. Theo thanks for your answer. It is indeed a matter of presentation. It is better to use Write-Output to get the full information like mklement0 said. So I changed the script into this: ```$Path = "./package-demo.json" $ContentObject = (Get-Content $Path ) | ConvertFrom-Json Write-Output $ContentObject $ContentObject.releaseDate = "today" Write-Output $ContentObject $ContentJson = $ContentObject | ConvertTo-Json Write-Output $ContentJson``` – Gert Rikkers Jul 27 '22 at 15:03

0 Answers0