在PowerShell中迭代JSON并删除JSON元素

3
以下是我的示例JSON文件。
  1. 遍历所有唯一属性
  2. 对于每个唯一属性,检查“x-visibility”:“Private”对象的值。如果这是“Private”,则删除整个对象。
我正在尝试做到这一点。
foreach ($p in $apijson.paths.PSObject.Properties.Value) {
    $get = $p.get.'x-qlik-visibility' -eq 'Private'

    if ($get -eq 'True') {
        Write-Host 'Removing Object  :'$p
        # Remove-ItemProperty -Name $p.get
        $apijsonNew.PSObject.Properties.Remove($p)
    }
}

{
  "swagger": "2.0",
  "info": {
    "title": "Open API specification for Repository Main API",
    "version": "20.8.0"
  },
  "basePath": "/qrs",
  "schemes": [
    "https"
  ],
  "paths": {
    "/about/api/default/analyticconnection": {
      "get": {
        "operationId": "2b781738-afd0-37a9-4248-84ec8d6be58c",
        "parameters": [
          {
            "name": "listentries",
            "in": "query",
            "required": false,
            "default": true,
            "type": "boolean",
            "allowEmptyValue": true
          }
        ],
        "responses": {
          "200": {
            "description": "OK",
            "schema": {
              "$ref": "#/definitions/AnalyticConnection"
            }
          }
        },
        "x-qlik-visibility": "Public",
        "x-qlik-stability": "Locked"
      }
    },
    "/app/{id}/replace": {
      "put": {
        "operationId": "a26375e5-5fb5-41e6-9117-ae145181caf0",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "type": "string",
            "format": "uuid"
          },
          {
            "name": "app",
            "in": "query",
            "required": true,
            "type": "string",
            "format": "uuid"
          }
        ],
        "responses": {
          "200": {
            "description": "OK",
            "schema": {
              "$ref": "#/definitions/App"
            }
          }
        },
        "x-qlik-visibility": "Public",
        "x-qlik-stability": "Locked"
      },
      "post": {
        "operationId": "3d02afa0-85d5-40bc-a25e-a39c416f46a1",
        "parameters": [
          {
            "name": "appchanges",
            "in": "body",
            "required": true,
            "schema": {
              "$ref": "#/definitions/EngineAppChange"
            }
          },
          {
            "name": "id",
            "in": "path",
            "required": true,
            "type": "string",
            "format": "uuid"
          }
        ],
        "responses": {
          "201": {
            "description": "Created",
            "schema": {
              "$ref": "#/definitions/EngineAppBundle"
            }
          }
        },
        "x-qlik-visibility": "Private",
        "x-qlik-stability": "Experimental"
      }
    },
    "/app/{id}/enginedatasegment": {
      "get": {
        "operationId": "8558e3fc-3a40-4910-b1ec-748f6aaa1322",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "type": "string",
            "format": "uuid"
          }
        ],
        "responses": {
          "200": {
            "description": "OK",
            "schema": {
              "$ref": "#/definitions/EngineAppBundle"
            }
          }
        },
        "x-qlik-visibility": "Private",
        "x-qlik-stability": "Experimental"
      }
    },
    "UserSyncTaskOperational": {
      "type": "object",
      "properties": {
        "id": {
          "type": "string",
          "format": "uuid"
        },
        "createdDate": {
          "type": "string",
          "format": "date-time"
        },
        "modifiedDate": {
          "type": "string",
          "format": "date-time"
        },
        "modifiedByUserName": {
          "type": "string"
        },
        "schemaPath": {
          "type": "string"
        },
        "privileges": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "lastExecutionResult": {
          "$ref": "#/definitions/ExecutionResultCondensed"
        },
        "nextExecution": {
          "type": "string",
          "format": "date-time"
        }
      },
      "x-qlik-stability": "Locked"
    },
    "UserSyncTaskOperationalCondensed": {
      "type": "object",
      "properties": {
        "id": {
          "type": "string",
          "format": "uuid"
        },
        "privileges": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "lastExecutionResult": {
          "$ref": "#/definitions/ExecutionResultCondensed"
        },
        "nextExecution": {
          "type": "string",
          "format": "date-time"
        }
      },
      "x-qlik-stability": "Locked"
    },
    "VirtualProxyConfig": {
      "type": "object",
      "properties": {
        "id": {
          "type": "string",
          "format": "uuid"
        },
        "createdDate": {
          "type": "string",
          "format": "date-time"
        },
        "modifiedDate": {
          "type": "string",
          "format": "date-time"
        },
        "modifiedByUserName": {
          "type": "string"
        },
        "schemaPath": {
          "type": "string"
        },
        "privileges": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "customProperties": {
          "type": "array",
          "items": {
            "$ref": "#/definitions/CustomPropertyValue"
          }
        },
        "prefix": {
          "type": "string",
          "maxLength": 100
        },
        "description": {
          "type": "string",
          "maxLength": 500
        },
        "authenticationModuleRedirectUri": {
          "type": "string",
          "format": "uri",
          "maxLength": 1024
        },
        "sessionModuleBaseUri": {
          "type": "string",
          "format": "uri",
          "maxLength": 1024
        },
        "loadBalancingModuleBaseUri": {
          "type": "string",
          "format": "uri",
          "maxLength": 1024
        },
        "useStickyLoadBalancing": {
          "type": "boolean"
        },
        "loadBalancingServerNodes": {
          "type": "array",
          "items": {
            "$ref": "#/definitions/ServerNodeConfigurationCondensed"
          }
        },
        "authenticationMethod": {
          "type": "integer",
          "default": 0,
          "enum": [
            0,
            1,
            2,
            3,
            4
          ],
          "x-enumNames": [
            "Ticket",
            "HeaderStaticUserDirectory",
            "HeaderDynamicUserDirectory",
            "SAML",
            "JWT"
          ]
        },
        "headerAuthenticationMode": {
          "type": "integer",
          "default": 0,
          "enum": [
            0,
            1,
            2,
            3
          ],
          "x-enumNames": [
            "NotAllowed",
            "StaticUserDirectory",
            "DynamicUserDirectory",
            "Undefined"
          ],
          "x-qlik-deprecated": true
        },
        "headerAuthenticationHeaderName": {
          "type": "string",
          "format": "http-header-name",
          "maxLength": 256
        },
        "headerAuthenticationStaticUserDirectory": {
          "type": "string",
          "maxLength": 256
        },
        "headerAuthenticationDynamicUserDirectory": {
          "type": "string",
          "maxLength": 256
        },
        "anonymousAccessMode": {
          "type": "integer",
          "default": 0,
          "enum": [
            0,
            1,
            2
          ],
          "x-enumNames": [
            "NoAnonymousUser",
            "AllowAnonymous",
            "AlwaysAnonymous"
          ]
        },
        "windowsAuthenticationEnabledDevicePattern": {
          "type": "string",
          "maxLength": 256,
          "minLength": 1
        },
        "sessionCookieHeaderName": {
          "type": "string",
          "format": "http-header-name",
          "maxLength": 256,
          "minLength": 1
        },
        "sessionCookieDomain": {
          "type": "string",
          "maxLength": 256,
          "minLength": 1
        },
        "additionalResponseHeaders": {
          "type": "string",
          "maxLength": 1024
        },
        "sessionInactivityTimeout": {
          "type": "integer",
          "format": "minutes",
          "maximum": 71582,
          "minimum": 1
        },
        "extendedSecurityEnvironment": {
          "type": "boolean"
        },
        "websocketCrossOriginWhiteList": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "defaultVirtualProxy": {
          "type": "boolean"
        },
        "tags": {
          "type": "array",
          "items": {
            "$ref": "#/definitions/TagCondensed"
          }
        },
        "samlMetadataIdP": {
          "type": "string",
          "maxLength": 100000
        },
        "samlHostUri": {
          "type": "string",
          "format": "uri",
          "maxLength": 1024
        },
        "samlEntityId": {
          "type": "string",
          "maxLength": 255
        },
        "samlAttributeUserId": {
          "type": "string",
          "maxLength": 255
        },
        "samlAttributeUserDirectory": {
          "type": "string",
          "maxLength": 255
        },
        "samlAttributeSigningAlgorithm": {
          "type": "integer",
          "default": 0,
          "enum": [
            0,
            1
          ],
          "x-enumNames": [
            "SHA1",
            "SHA256"
          ]
        },
        "samlAttributeMap": {
          "type": "array",
          "items": {
            "$ref": "#/definitions/VirtualProxyConfigSamlAttributeMapItem"
          }
        },
        "jwtAttributeUserId": {
          "type": "string",
          "maxLength": 255
        },
        "jwtAttributeUserDirectory": {
          "type": "string",
          "maxLength": 255
        },
        "jwtPublicKeyCertificate": {
          "type": "string",
          "maxLength": 100000
        },
        "jwtAttributeMap": {
          "type": "array",
          "items": {
            "$ref": "#/definitions/VirtualProxyConfigJwtAttributeMapItem"
          }
        },
        "magicLinkHostUri": {
          "type": "string",
          "format": "uri",
          "maxLength": 1024
        },
        "magicLinkFriendlyName": {
          "type": "string",
          "maxLength": 255
        },
        "samlSlo": {
          "type": "boolean"
        }
      },
      "required": [
        "sessionCookieHeaderName",
        "sessionInactivityTimeout",
        "defaultVirtualProxy"
      ],
      "x-qlik-stability": "Locked"
    },
    "VirtualProxyConfigCondensed": {
      "type": "object",
      "properties": {
        "id": {
          "type": "string",
          "format": "uuid"
        },
        "privileges": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "prefix": {
          "type": "string",
          "maxLength": 100
        },
        "description": {
          "type": "string",
          "maxLength": 500
        },
        "authenticationModuleRedirectUri": {
          "type": "string",
          "format": "uri",
          "maxLength": 1024
        },
        "sessionModuleBaseUri": {
          "type": "string",
          "format": "uri",
          "maxLength": 1024
        },
        "loadBalancingModuleBaseUri": {
          "type": "string",
          "format": "uri",
          "maxLength": 1024
        },
        "useStickyLoadBalancing": {
          "type": "boolean"
        },
        "loadBalancingServerNodes": {
          "type": "array",
          "items": {
            "$ref": "#/definitions/ServerNodeConfigurationCondensed"
          }
        },
        "authenticationMethod": {
          "type": "integer",
          "default": 0,
          "enum": [
            0,
            1,
            2,
            3,
            4
          ],
          "x-enumNames": [
            "Ticket",
            "HeaderStaticUserDirectory",
            "HeaderDynamicUserDirectory",
            "SAML",
            "JWT"
          ]
        },
        "headerAuthenticationMode": {
          "type": "integer",
          "default": 0,
          "enum": [
            0,
            1,
            2,
            3
          ],
          "x-enumNames": [
            "NotAllowed",
            "StaticUserDirectory",
            "DynamicUserDirectory",
            "Undefined"
          ],
          "x-qlik-deprecated": true
        },
        "headerAuthenticationHeaderName": {
          "type": "string",
          "format": "http-header-name",
          "maxLength": 256
        },
        "headerAuthenticationStaticUserDirectory": {
          "type": "string",
          "maxLength": 256
        },
        "headerAuthenticationDynamicUserDirectory": {
          "type": "string",
          "maxLength": 256
        },
        "anonymousAccessMode": {
          "type": "integer",
          "default": 0,
          "enum": [
            0,
            1,
            2
          ],
          "x-enumNames": [
            "NoAnonymousUser",
            "AllowAnonymous",
            "AlwaysAnonymous"
          ]
        },
        "windowsAuthenticationEnabledDevicePattern": {
          "type": "string",
          "maxLength": 256,
          "minLength": 1
        },
        "sessionCookieHeaderName": {
          "type": "string",
          "format": "http-header-name",
          "maxLength": 256,
          "minLength": 1
        },
        "sessionCookieDomain": {
          "type": "string",
          "maxLength": 256,
          "minLength": 1
        },
        "additionalResponseHeaders": {
          "type": "string",
          "maxLength": 1024
        },
        "sessionInactivityTimeout": {
          "type": "integer",
          "format": "minutes",
          "maximum": 71582,
          "minimum": 1
        },
        "extendedSecurityEnvironment": {
          "type": "boolean"
        },
        "websocketCrossOriginWhiteList": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "defaultVirtualProxy": {
          "type": "boolean"
        },
        "tags": {
          "type": "array",
          "items": {
            "$ref": "#/definitions/TagCondensed"
          }
        },
        "samlMetadataIdP": {
          "type": "string",
          "maxLength": 100000
        },
        "samlHostUri": {
          "type": "string",
          "format": "uri",
          "maxLength": 1024
        },
        "samlEntityId": {
          "type": "string",
          "maxLength": 255
        },
        "samlAttributeUserId": {
          "type": "string",
          "maxLength": 255
        },
        "samlAttributeUserDirectory": {
          "type": "string",
          "maxLength": 255
        },
        "samlAttributeSigningAlgorithm": {
          "type": "integer",
          "default": 0,
          "enum": [
            0,
            1
          ],
          "x-enumNames": [
            "SHA1",
            "SHA256"
          ]
        },
        "samlAttributeMap": {
          "type": "array",
          "items": {
            "$ref": "#/definitions/VirtualProxyConfigSamlAttributeMapItem"
          }
        },
        "jwtAttributeUserId": {
          "type": "string",
          "maxLength": 255
        },
        "jwtAttributeUserDirectory": {
          "type": "string",
          "maxLength": 255
        },
        "jwtPublicKeyCertificate": {
          "type": "string",
          "maxLength": 100000
        },
        "jwtAttributeMap": {
          "type": "array",
          "items": {
            "$ref": "#/definitions/VirtualProxyConfigJwtAttributeMapItem"
          }
        },
        "magicLinkHostUri": {
          "type": "string",
          "format": "uri",
          "maxLength": 1024
        },
        "magicLinkFriendlyName": {
          "type": "string",
          "maxLength": 255
        },
        "samlSlo": {
          "type": "boolean"
        }
      },
      "required": [
        "sessionCookieHeaderName",
        "sessionInactivityTimeout",
        "defaultVirtualProxy"
      ],
      "x-qlik-stability": "Locked"
    },
    "VirtualProxyConfigJwtAttributeMapItem": {
      "type": "object",
      "properties": {
        "id": {
          "type": "string",
          "format": "uuid"
        },
        "createdDate": {
          "type": "string",
          "format": "date-time"
        },
        "modifiedDate": {
          "type": "string",
          "format": "date-time"
        },
        "modifiedByUserName": {
          "type": "string"
        },
        "schemaPath": {
          "type": "string"
        },
        "jwtAttribute": {
          "type": "string",
          "maxLength": 255
        },
        "senseAttribute": {
          "type": "string",
          "maxLength": 255
        },
        "isMandatory": {
          "type": "boolean"
        }
      },
      "required": [
        "jwtAttribute",
        "senseAttribute",
        "isMandatory"
      ]
    },
    "VirtualProxyConfigSamlAttributeMapItem": {
      "type": "object",
      "properties": {
        "id": {
          "type": "string",
          "format": "uuid"
        },
        "createdDate": {
          "type": "string",
          "format": "date-time"
        },
        "modifiedDate": {
          "type": "string",
          "format": "date-time"
        },
        "modifiedByUserName": {
          "type": "string"
        },
        "schemaPath": {
          "type": "string"
        },
        "samlAttribute": {
          "type": "string",
          "maxLength": 255
        },
        "senseAttribute": {
          "type": "string",
          "maxLength": 255
        },
        "isMandatory": {
          "type": "boolean"
        }
      },
      "required": [
        "samlAttribute",
        "senseAttribute",
        "isMandatory"
      ]
    }
  }
}

1
你忘记写问题是什么了。 - Palle Due
我的问题是我想从我的JSON文件中删除"x-qlik-visibility": "Private"。 - Swati Jha
1
这是你的目标。问题是什么(即什么不按照你的预期工作,以及它具体是如何不按照你的预期工作的)? - Ansgar Wiechers
问题是我不熟悉PowerShell,不知道应该如何处理。 - Swati Jha
你的尝试输出了什么?有错误/没有替换吗? :) - xxxvodnikxxx
显示剩余2条评论
2个回答

10
想从一个 JSON 文件中删除一个元素。
@Theo 的方法对我来说并不成功,所以我使用以下方法将我的文件从 JSON 转换为对象:
$fich_convert = Get-Content -Path $fichier -Raw | ConvertFrom-Json

然后我移除了我想要的内容:
 $fich_convert.PSObject.Properties.Remove('_id')

最后将其转换回JSON格式:
 $fich_convert | ConvertTo-Json -Depth 100 | Out-File $fichier -Force

这对我起作用了。 希望这会有所帮助。

5
尽管这个问题已经快一个月了,但我看到还没有答案,所以我来试一试:
$inputFile  = '<THE FULL PATH AND FILENAME TO YOUR JSON FILE>'
$outputFile = '<THE FULL PATH AND FILENAME FOR THE OUTPUT JSON FILE>'

$apijson = Get-Content -Path $inputFile -Raw | ConvertFrom-Json

# for safety, first make a copy of the original .paths object
$newPaths = $apijson.paths

# next remove the elements that contain 'Private' in the 'x-qlik-visibility' property
foreach ($element in $newPaths.PSObject.Properties) {
    $objName = $element.Name
    $objValue = $element.Value
    $objProperties = $objValue.PSObject.Properties
    foreach ($prop in $objProperties) {
        if ($prop.Value.'x-qlik-visibility' -eq 'Private') {
            $propName = $prop.Name
            $objProperties.Remove($propName)
            Write-Host "Removed object $objName -- $propName"
        }
    }
}

# now overwrite the $apijson.paths with this cleaned up version
$apijson.paths = $newPaths

# I assume you want to convert it back to a .JSON file??
$apijson | ConvertTo-Json -Depth 100 | Set-Content -Path $outputFile -Force

请注意,PowerShell 生成的 JSON 文件没有很好的外观。如果您想做得更好,请在此处找到我的 Format-Json 函数:PowerShell JSON 添加值格式
使用方法如下:
$apijson | ConvertTo-Json -Depth 100 | Format-Json | Set-Content -Path $outputFile -Force

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接