在 PowerShell 中迭代 JSON 并删除 JSON 元素 [英] Iterate over JSON and remove JSON element in PowerShell

查看:54
本文介绍了在 PowerShell 中迭代 JSON 并删除 JSON 元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面粘贴的是我的示例 JSON 文件.

Pasted below is my sample JSON file.

  1. 遍历所有unqiue属性
  2. 对于每个唯一的属性,检查 "x-visibility": "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"
      ]
    }
  }
}

推荐答案

想要从 json 文件中删除一个元素.

Wanted to remove an element from a json file.

@Theo 的方法对我来说并不成功,所以我使用以下方法将我的文件从 JSON 转换为对象:

@Theo 's method wasn't a success for me so, I converted my file from JSON to object using :

$fich_convert = Get-Content -Path $fichier -Raw | ConvertFrom-Json

然后我删除了我想要的:

Then i removed what i wanted :

 $fich_convert.PSObject.Properties.Remove('_id')

最后将其转换回 JSON :

And finaly converted it back to JSON :

 $fich_convert | ConvertTo-Json -Depth 100 | Out-File $fichier -Force

这对我有用.希望这会有所帮助.

This worked for me. Hope this will help.

这篇关于在 PowerShell 中迭代 JSON 并删除 JSON 元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆