繁体   English   中英

遍历JSON并在PowerShell中删除JSON元素

[英]Iterate over JSON and remove JSON element in PowerShell

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

  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"
      ]
    }
  }
}

尽管这个问题已经存在了将近一个月,但我发现还没有答案,所以我将尝试一下:

$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增加值formate

像这样使用它:

$apijson | ConvertTo-Json -Depth 100 | Format-Json | Set-Content -Path $outputFile -Force

想要从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

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

暂无
暂无

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

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