繁体   English   中英

如何从另一个Json文件PowerShell复制最新的Json数据?

[英]How to copy latest Json data from another Json file PowerShell?

我有一个source.Json文件,如下所示

{
  "kind": "sql#backupRunsList",
  "items": [
    {
      "kind": "sql#backupRun",
      "status": "SUCCESSFUL",
      "enqueuedTime": "2023-01-11T00:33:21.903Z",
      "id": "1673391600000",
      "startTime": "2023-01-11T00:33:21.949Z",
      "endTime": "2023-01-11T00:38:47.459Z",
      "type": "AUTOMATED",
      "windowStartTime": "2023-01-10T23:00:00Z",
      "instance": "instance",
      "selfLink": "https://sqladmin.googleapis.com/v1/projects/project/instances/instance/backupRuns/1673391600000",
      "location": "us",
      "backupKind": "SNAPSHOT"
    },
    {
      "kind": "sql#backupRun",
      "status": "SUCCESSFUL",
      "enqueuedTime": "2023-01-09T23:36:39.776Z",
      "id": "1673305200000",
      "startTime": "2023-01-09T23:36:39.826Z",
      "endTime": "2023-01-09T23:42:05.542Z",
      "type": "AUTOMATED",
      "windowStartTime": "2023-01-09T23:00:00Z",
      "instance": "instance ",
      "selfLink": "https://sqladmin.googleapis.com/v1/projects/project/instances/instance/backupRuns/1673305200000",
      "location": "us",
      "backupKind": "SNAPSHOT"
    },

我下面有一个 target.json 文件

{
  "restoreBackupContext":
  {
    "backupRunId": 123456,
    "project": "project",
    "instanceId": "instance"
  }
}

如何复制数据“id”:“1673391600000”,从 source.json 到“backupRunId”:值到 target.json

在目标 backupRunId 中,值“123456”应使用电源 shell 替换为“1673391600000”值。

从源文件获取 id 值,如何复制到目标文件

$content=Get-Content -Path source.json
$JsonData=$content | ConvertFrom-Json
$JsonData.Items.id[0]

请任何人建议

如果您只想处理$JsonData.Items中的第一项并更新单个目标 json 文件

# Grab json text from source file and convert to PSobjects
$source = Get-Content .\source.json | ConvertFrom-Json

# Grab json text from target file and convert to PSobjects
$target = Get-Content .\target.json | ConvertFrom-Json

# Update $target object with id from $source object
$target.restoreBackupContext.backupRunId = $source.items[0].id

# Take updated $target object, convert back to json and save back to target.json file
$target | ConvertTo-Json | Set-Content -Path .\target.json

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM