2016-07-05 25 views
-1

以下のサンプルネストされたjsonレスポンスがあります。このレスポンスを特定の値でCSVファイルに変換する必要があります。以下のサンプルは、ネストされたJSON応答:私は "PROCESSConfiguration "" からの応答をする必要があるネストされたJsonレスポンスをpowershellのcsvに変換する必要があります

{ 
    "transaction": { 
    "id": "TestTransID", 
    "testCode": "NEW", 
    "TestStatus": "SUCCESS", 
    "client": { 
     "TestNumber": "112112111" 
    }, 
    "subject": { 
     "individual": { 
     "additionalAttributes": { 
      "extraid": "787877878" 
     }, 
     "addressList": [ 
      { 
      "city": "New York", 
      "country": { 
       "name": "United States" 
      }, 
      "postalCode": "123456789", 
      "stateOrProvince": { 
       "codeValue": "NY" 
      } 
      } 
     ], 
     "gender": "F", 
     "identificationDocumentList": [ 
      { 
      "number": "1214558520", 
      "type": "TestId" 
      } 
     ], 
     "name": { 
      "firstName": "Qusay TestFull", 
      "lastName": "TestLast", 
      "middleName": "Middle 3" 
     } 
     } 
    }, 
    "PROCESSConfiguration": { 
     "id": 1 
    }, 
    "testProductList": [ 
     { 
     "product": { 
      "id": 00, 
      "name": "Test PROCESS", 
      "productCode": "EFG", 
      "disclaimer": "TestDisclaimer" 
     }, 
     "testSourceResponseList": [ 
      { 
      "testSource": { 
       "id": 1, 
       "name": "TEST" 
      }, 
      "testSourceRecordList": [ 
       { 
       "type": "TestRecord", 
       "alertReasonCode": "TESTS", 
       "alertReasonDescription": "ACTION LIST HIT - TEST", 
       "testSource": "TEST", 
       "varListNameFull": "TEST FULL NAME", 
       "varListNameShort": "TEST SHORT", 
       "varProgList": [ 
        "SHORT" 
       ], 
       "varListId": "3421", 
       "subject": { 
        "individual": { 
        "TestScore": { 
         "TestScore": 100, 
         "triggeredRule": "TestRule" 
        }, 
        "aNameList": [ 
         { 
         "fullName": " TestNameA", 
         "lastName": "TestNameA" 
         }, 
         { 
         "firstName": "TestFirst", 
         "fullName": "TestFirst HUSAYN", 
         "lastName": "TestLast" 
         }, 
         { 
         "firstName": "TestFirst", 
         "fullName": "TestFull", 
         "lastName": "TestLast" 
         }, 
         { 
         "firstName": "TestFirst", 
         "fullName": "TestFull", 
         "lastName": "TestLast" 
         } 
        ], 
        "birthList": [ 
         { 
         "dateOfBirth": "12 Apr 1910", 
         "dateOfBirthVerified": "true" 
         } 
        ], 
        "name": { 
         "firstName": "TestFirst", 
         "fullName": "TestFull", 
         "lastName": "TestLast" 
         }, 
        "varNationality": [ 
         { 
         "verified": "true" 
         } 
        ], 
        "remarks": "remark1" 
        } 
       } 
       }, 
       { 
       "testSource": "TEST", 
       "varListNameFull": "TEST FULL", 
       "varListNameShort": "TEST SHORT", 
       "varProgList": [ 
        "XYZ" 
       ], 
       "varListId": "1234", 
       "subject": { 
        "individual": { 
        "overallScore": { 
         "TestScore": 100, 
         "triggeredRule": "Testing" 
        }, 
        "birthList": [ 
         { 
         "dateOfBirth": "1965", 
         }, 
         { 
         "dateOfBirth": "1966", 
         } 
        ], 
        "name": { 
         "firstName": "TestFirst", 
         "fullName": "TestFull", 
         "lastName": "TestLast", 
        }, 
        "varNationality": [ 
         { 
         "verified": "true" 
         } 
        ], 
        "remarks": "REMARK2" 
        } 
       } 
       } 
      ] 
      } 
     ] 
     } 
    ], 
    } 
} 

:{ "ID" 1" 行#40からu'llにコード上に取る場合メモ帳++。

はまた、私は、私はまだのために求められているものか不明だ

+0

それが掲載されたときにあなたのJSONがマングルされました。そのデータに対して特定の回答が必要な場合は、修正する必要があります。また、CSVとして表現された「ネストされた」値がどのようにしてCSVがフラットフォーマットであるかを記述する必要があります。この場合は、firstName、dateOfBirth、postalCodeなどの列ヘッダーがあります。 – stevlars

+0

json形式を更新しました。私は応答が必要なときにも方法を更新しました。 –

答えて

0

最初の名前のように、VAR値に対する応答を必要とし、最後の名前フルネーム、DOBなど。 JSONファイル内の各要素の完全修飾パスが必要であると仮定します。

まず、http://jsonlint.com/に基づいてJSONを少し調整しました。

それに基づいて、あなたが投稿したJSONのようなONE OBJECTのために働く概念の証明をしました。これはこのケースのために働く。私は、複数のオブジェクトを処理するロジックをラップして、オブジェクトが開始されたときと次のオブジェクトが開始されたときを前提にしました。また、一般的なケースを扱うために、複数のプロパティ(すなわち、Get-ProcessのThreads)を含むネストされたシリーズ/配列を扱うロジックを追加する必要があります。値の配列を持つ単一の要素(この場合は.transaction.varProgListなど)は、それらの間に ';'を入れることで処理されます。

CSVファイルは、すべてのオブジェクトが対称(同じプロパティを持つ)であることを前提としています。各オブジェクトのプロパティが他のオブジェクトのプロパティと揃うことを確認するチェックはありません。ネストされた系列の処理はこれに関連していることに注意してください。あなたは次のように$aが調整JSONコンテンツであると機能を展開し、NestedProperty.ps1として保存される。ここで、[System.Collections.ICollection]セクションとREPRO

(Get-Process r*) | Select-Object Name,Threads | Expand-NestedProperty | Out-File .\t.csv -Width 100000ような何かをしようが行くコメント解除することによって、この例を参照のこと。

# Load the function 
. .\Expand-NestedProperty.ps1 
# Create PowerShell object(s) based on the JSON 
$b = $a | ConvertFrom-Json 
# Create a file with the CSV contents. 
$b | Expand-NestedProperty | Out-File -FilePath .\my.csv -Width 100000 

保存し、このように展開し、NestedProperty.ps1

function Expand-NestedProperty { 
    [CmdletBinding()] 
    param (
     [Parameter(Position=0, 
        Mandatory=$true, 
        ValueFromPipeline=$true, 
        ValueFromPipelineByPropertyName=$true, 
        ValueFromRemainingArguments=$false, 
        HelpMessage='Object required...')] 
     $InputObject 
    ) 
    begin { 
     function ExpandNestedProperty { 
      [CmdletBinding()] 
      param (
       [Parameter(Position=0, 
          Mandatory=$true, 
          ValueFromPipeline=$true, 
          ValueFromPipelineByPropertyName=$true, 
          ValueFromRemainingArguments=$false, 
          HelpMessage='Object required...')] 
       $InputObject, 
       [Parameter(Position=1, 
          Mandatory=$false, 
          ValueFromPipeline=$false, 
          ValueFromPipelineByPropertyName=$true, 
          ValueFromRemainingArguments=$false, 
          HelpMessage='String required...')] 
       [string] 
       $FullyQualifiedName = "" 
      ) 
      begin { 
       $localResults [email protected]() 
       $FQN = $FullyQualifiedName 
       $nestedProperties = $null 
      } 
      process { 
       foreach ($obj in $InputObject.psobject.Properties) { 
        if ($(try {$obj.Value[0] -is [PSCustomObject]} catch {$false})) { # Catch 'Cannot index into a null array' for null values 
         # Nested properties 
         $FQN = "$($FullyQualifiedName).$($obj.Name)" 
         $nestedProperties = $obj.value | ExpandNestedProperty -FullyQualifiedName $FQN 
        } 
        elseif ($obj.Value -is [array]) { 
         # Array property 
         $FQN = "$($FullyQualifiedName).$($obj.Name)" 
         [psobject]$nestedProperties = @{ 
          $FQN = ($obj.Value -join ';') 
         } 
        } 
        # Example of how to deal with generic case. 
        # This needed for the Get-Process values ([System.Collections.ReadOnlyCollectionBase] and [System.Diagnostics.FileVersionInfo]) that are not [array] collection type. 
<# 
        elseif ($obj.Value -is [System.Collections.ICollection]) { 
         # Nested properties 
         $FQN = "$($FullyQualifiedName).$($obj.Name)" 
         $nestedProperties = $obj.value | ExpandNestedProperty -FullyQualifiedName $FQN 
        } 
#> 
        else { # ($obj -is [PSNoteProperty]) for this case, but could be any type 
         $FQN = "$($FullyQualifiedName).$($obj.Name)" 
         [psobject]$nestedProperties = @{ 
          $FQN = $obj.Value 
         } 
        } 
        $localResults += $nestedProperties 
       } #foreach $obj 
      } 
      end { 
       [pscustomobject]$localResults 
      } 
     } # function ExpandNestedProperty 
     $objectNumber = 0 
     $firstObject = @() 
     $otherObjects = @() 
    } 
    process { 
     if ($objectNumber -eq 0) { 
      $objectNumber++ 
      $firstObject = $InputObject[0] | ExpandNestedProperty 
     } 
     else { 
      if ($InputObject -is [array]) { 
       foreach ($nextInputObject in $InputObject[1..-1]) { 
        $objectNumber++ 
        $otherObjects += ,($nextInputObject | ExpandNestedProperty) 
       } 
      } 
      else { 
       $objectNumber++ 
       $otherObjects += ,($InputObject | ExpandNestedProperty) 
      } 
     } 
    } 
    end { 
     # Output CSV header and a line for each object which was the specific requirement here. 
     # Could create an array of objects using $firstObject + $otherObjects that is then piped to Export-CSV if we want a generic case. 
     Write-Output "`"$($firstObject.Keys -join '","')`"" 
     Write-Output "`"$($firstObject.Values -join '","')`"" 
     foreach ($otherObject in $otherObjects) { 
      Write-Output "`"$($otherObject.Values -join '","')`"" 
     } 
    } 
} # function Expand-NestedProperty 
関連する問題