Powershell command ConvertFrom-Json gives empty objects

34 Views Asked by At

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=}
0

There are 0 best solutions below