簡體   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