概要
Azure REST APIで取得した、JSON形式のデータから、抽出するのに困っています。
背景
Azure REST APIを利用して、Azure CDNのPOP一覧を取得したときに、JSON形式で出てくるのですが、
そこから、IPアドレスとプレフィックス長を抽出して、下のような形式に加工したいと考えています。
IP Address/Prefix Length
(例) 120.132.137.0/25
このPOP一覧は下記のように出てきます(文字数の関係で、一部略)
JSON
1{ 2 "value":[ 3 { 4 "name":"Standard_Verizon","id":"/providers/Microsoft.Cdn/edgenodes/Standard_Verizon","type":"Microsoft.Cdn/edgenodes","properties":{ 5 "ipAddressGroups":[ 6 { 7 "deliveryRegion":"All","ipv4Addresses":[ 8 { 9 "baseIpAddress":"5.104.64.0","prefixLength":21 10 },{ 11 "baseIpAddress":"46.22.64.0","prefixLength":20 12 },{ 13 "baseIpAddress":"61.221.181.64","prefixLength":26 14 },{ 15 "baseIpAddress":"68.232.32.0","prefixLength":20 16 },{ 17 "baseIpAddress":"72.21.80.0","prefixLength":20 18 },{ 19 "baseIpAddress":"88.194.45.128","prefixLength":26 20 },{ 21 "baseIpAddress":"93.184.208.0","prefixLength":20 22 },{ 23 "baseIpAddress":"101.226.203.0","prefixLength":24 24 },{ 25 "baseIpAddress":"88.194.47.224","prefixLength":27 26 },{ 27 "baseIpAddress":"203.66.205.0","prefixLength":24 28 } 29 ],"ipv6Addresses":[ 30 { 31 "baseIpAddress":"2001:2011:c002::","prefixLength":48 32 },{ 33 "baseIpAddress":"2001:2040:c006::","prefixLength":48 34 },{ 35 "baseIpAddress":"2001:2060:bffb::","prefixLength":48 36 },{ 37 "baseIpAddress":"2001:b032:c101::","prefixLength":48 38 },{ 39 "baseIpAddress":"2405:8f00:edca::","prefixLength":48 40 },{ 41 "baseIpAddress":"2405:8f00:edcb::","prefixLength":48 42 },{ 43 "baseIpAddress":"2606:2800::","prefixLength":32 44 },{ 45 "baseIpAddress":"2600:40ff:fffb::","prefixLength":56 46 },{ 47 "baseIpAddress":"2a02:16d8:103::","prefixLength":48 48 },{ 49 "baseIpAddress":"2600:40fc::","prefixLength":32 50 } 51 ] 52 } 53 ] 54 } 55 },{ 56 57 "name":"Custom_Verizon","id":"/providers/Microsoft.Cdn/edgenodes/Custom_Verizon","type":"Microsoft.Cdn/edgenodes","properties":{ 58 "ipAddressGroups":[ 59 { 60 "deliveryRegion":"All","ipv4Addresses":[ 61 { 62 "baseIpAddress":"5.104.64.0","prefixLength":21 63 },{ 64 "baseIpAddress":"46.22.64.0","prefixLength":20 65 },{ 66 "baseIpAddress":"61.221.181.64","prefixLength":26 67 },{ 68 "baseIpAddress":"68.232.32.0","prefixLength":20 69 },{ 70 "baseIpAddress":"72.21.80.0","prefixLength":20 71 },{ 72 "baseIpAddress":"88.194.45.128","prefixLength":26 73 },{ 74 "baseIpAddress":"203.66.205.0","prefixLength":24 75 } 76 ],"ipv6Addresses":[ 77 { 78 "baseIpAddress":"2001:2011:c002::","prefixLength":48 79 },{ 80 "baseIpAddress":"2001:2040:c006::","prefixLength":48 81 },{ 82 "baseIpAddress":"2001:2060:bffb::","prefixLength":48 83 },{ 84 "baseIpAddress":"2001:b032:c101::","prefixLength":48 85 },{ 86 "baseIpAddress":"2405:8f00:edca::","prefixLength":48 87 },{ 88 "baseIpAddress":"2405:8f00:edcb::","prefixLength":48 89 },{ 90 "baseIpAddress":"2606:2800::","prefixLength":32 91 },{ 92 "baseIpAddress":"2600:40ff:fffb::","prefixLength":56 93 },{ 94 "baseIpAddress":"2a02:16d8:103::","prefixLength":48 95 },{ 96 "baseIpAddress":"2600:40fc::","prefixLength":32 97 } 98 ] 99 } 100 ] 101 } 102 } 103 ] 104}
上のJSONから、すべてのIPアドレスとプレフィックス長を抽出したいのですが、PowerShellでの加工法がわかりません。
jqが使える環境ならば、次のようにしたいのです。
jq
1# IPv4 2jq -r '.value[].properties.ipAddressGroups[].ipv4Addresses[]| [.baseIpAddress, .prefixLength]' 3 4# IPv6 5jq -r '.value[].properties.ipAddressGroups[].ipv6Addresses[]| [.baseIpAddress, .prefixLength]'
ローカルでしたら、jqをインストールすればよいのですが、
Azure AutomationのRunbookで動かすことを目的としているので、そうもいかず、というところです。
行き詰まっているところ
ConvertFrom-JsonでPSObjectに変換しても、階層が問題になっているのか、うまくできません。
PowerShell
1$obj = ConvertFrom-Json $response 2echo $obj 3 4value 5----- 6{@{name=Standard_Verizon; id=/providers/Microsoft.Cdn/edgenodes/Standard_Verizon; type=Microsoft.Cdn/edgenodes; properties=}, @{name=Premium_Verizon; id=/providers/Microsoft.Cdn/edgenodes/Premium...
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。