English 中文(简体)
如何用电将新的json钥匙和数值乘以现有卷宗中的中间部分 未重写整个档案的壳牌
原标题:How to append new json key and value pair to a middle of existing .json file using PowerShell without rewriting entire file

我在Gite Hub有一份与下文类似的现有杰森档案。

{
    "quiz": {
        "sport": {
            "q1": {
                "question": "Which one is correct team name in NBA?",
                "options": [
                    "New York Bulls",
                    "Los Angeles Kings",
                    "Golden State Warriros",
                    "Huston Rocket"
                ],
                "answer": "Huston Rocket"
            }
        },
        "maths": {
            "q1": {
                "question": "5 + 7 = ?",
                "options": [
                    "10",
                    "11",
                    "12",
                    "13"
                ],
                "answer": "12"
            },
            "q2": {
                "question": "12 - 8 = ?",
                "options": [
                    "1",
                    "2",
                    "3",
                    "4"
                ],
                "answer": "4"
            }
        }
    }
}

我想在使用强国歌的“数学”上增加新的关键和价值。 我在把 g带回我的当地司后,在指挥下受审。

$FilePath =  C:Users	est.json

$y = Get-Content $FilePath | ConvertFrom-Json
$value = @`
   {
                "question": "12 - 6 = ?",
                "options": [
                    "1",
                    "2",
                    "3",
                    "6"
                ],
                "answer": "6"
      }
`@
$resultObject = $value | ConvertFrom-Json
$y.quiz.maths | Add-Member -MemberType NoteProperty -Name "q3" -Value $resultObject
$y | ConvertTo-Json -Depth 3 | Set-Content -Path $FilePath

随后承诺并推动这些变化。 当我检查主要分支与我的特工部门之间的比较时,由于新的白色空间,整个档案已经改写。

注:

  1. "-compress" command did not help here
  2. If I merge and again raise a PR with new values, It gives that specific commit only but this will continue unwanted whitespaces

问题;

页: 1 如果没有不想要的白天空间,是否有这样做的适当途径。

2. Can we do it without transforming into PowerShell Object. 我知道,我们可以将它变成一个使用——Raw方案,然后我们如何在新财产之上增加,并作出只有具体改动的单一承诺。

3。 除以上2种选择外,是否还有其他办法为使用PowerShell的案卷增添新的价值?

问题回答

We can simply update the Powershell version to 7+ and use -Depth option when converting to PowerShell object and use same Depth option when converting back to the JSON file. This way, it fixed the issue

这似乎好像是使用Class,其中界定了你的“问题”结构,这样就可以更容易地在你的Json档案中添加新的内容。

First step is to define the Class:

class Question {
    [string] $question
    [string[]] $options
    [string] $answer

    Question([string] $question) {
        $this.question = $question
    }

    [Question] AddOptions([string[]] $options) {
        $this.options = $options
        return $this
    }

    [Question] AddAnswer([string] $answer) {
        $this.answer = $answer
        return $this
    }

    [psnoteproperty] ToNoteProperty([string] $name) {
        return [psnoteproperty]::new($name, $this)
    }
}

既然如此,你可以重新使用(提出新的问题):

$newQuestion = [Question]::new( 12 - 6 = ? ).
    AddOptions((1, 2, 3, 6)).
    AddAnswer(6).
    ToNoteProperty( q3 )

最后,你可以把这个新问题同你的Json一样:

$json = Get-Content path	ojsonfile.json -Raw | ConvertFrom-Json
# `.quiz.maths` is the variable path here, the rest would remain the same
$json.quiz.maths.PSObject.Properties.Add($newQuestion)
$json | ConvertTo-Json -Depth 4 | Set-Content path	ojsonfile.json

Json从这个例子中得出以下结论:

{
  "quiz": {
    "sport": {
      "q1": {
        "question": "Which one is correct team name in NBA?",
        "options": [
          "New York Bulls",
          "Los Angeles Kings",
          "Golden State Warriros",
          "Huston Rocket"
        ],
        "answer": "Huston Rocket"
      }
    },
    "maths": {
      "q1": {
        "question": "5 + 7 = ?",
        "options": [
          "10",
          "11",
          "12",
          "13"
        ],
        "answer": "12"
      },
      "q2": {
        "question": "12 - 8 = ?",
        "options": [
          "1",
          "2",
          "3",
          "4"
        ],
        "answer": "4"
      },
      "q3": {
        "question": "12 - 6 = ?",
        "options": [
          "1",
          "2",
          "3",
          "6"
        ],
        "answer": "6"
      }
    }
  }
}




相关问题
git confusion - cloning a repo is returning a past version

Im having some confusion with my git usage. I cloned a repo from one comp to the other, and the new clone is the state of the original that was active some time ago. So its cloning a past version. ...

Appropriate strategy for tagging and hotfixing with git

I was wondering if the strategy I m using for tagging and hotfixing tags (which then I use for deploying rails applications) with git is appropriate. For tagging I just tag a commit of the master ...

Tips on upgrading CVS to git/hg?

We still use CVS, I use git and hg for my personal use though I m still a novice at both, but I realize they re much more modern and better, faster, distributed, etc. It s just everyone is so ...

Using Git in a TFS shop

Using Git at home has spoiled me - I now find using TFS at work to be a bit of a drag and want to explore the possibility of using Git locally and syncing somehow with TFS. I figure there are a few ...