简体   繁体   中英

Receive IPs from JSON file

I am trying to get all IPs from a JSON file using Python 2.7.5 However I can not manage to do it correctly. Do someone have an advice how I can receive all IPs from ('addressPrefixes') in a txt file?

Here is the code I already got to download the json file:

import urllib
import json
from urllib import urlopen


testfile = urllib.URLopener()
testfile.retrieve("https://download.microsoft.com/download/7/1/D/71D86715-5596-4529-9B13- 
DA13A5DE5B63/ServiceTags_Public_20210426.json", "AzureIPs.json")

print("---SUCCESSFULLY RECEIVED MICROSOFT AZURE IPS---")

with open('AzureIPs.json','r') as f:
     data = json.load(f)

the JSON file contains many IPs and IP Ranges and looks like this:

 {
 "changeNumber": 145,
 "cloud": "Public",
 "values": [
 {
    "name": "ActionGroup",
    "id": "ActionGroup",
    "properties": {
       "changeNumber": 9,
       "region": "",
       "regionId": 0,
       "platform": "Azure",
       "systemService": "ActionGroup",
       "addressPrefixes": [
          "13.66.60.119/32",
          "13.66.143.220/30",
          "13.66.202.14/32",
          "13.66.248.225/32",
          "13.66.249.211/32",
          "13.67.10.124/30",
          "13.69.109.132/30",
          "13.71.199.112/30",
          "13.77.53.216/30",
          "13.77.172.102/32",
          "13.77.183.209/32",
          "13.78.109.156/30",
          "13.84.49.247/32",         
          "2603:1030:c06:400::978/125",
          "2603:1030:f05:402::178/125",
          "2603:1030:1005:402::178/125",
          "2603:1040:5:402::178/125",
          "2603:1040:207:402::178/125",
          "2603:1040:407:402::178/125",
          "2603:1040:606:402::178/125",
          "2603:1040:806:402::178/125",
          "2603:1040:904:402::178/125",
          "2603:1040:a06:402::178/125",
          "2603:1040:b04:402::178/125",
          "2603:1040:c06:402::178/125",
          "2603:1040:d04:800::f8/125",
          "2603:1040:f05:402::178/125",
          "2603:1040:1104:400::178/125",
          "2603:1050:6:402::178/125",
          "2603:1050:403:400::1f8/125"
      ],
       "networkFeatures": [
        "API",
        "NSG",
        "UDR",
        "FW"
      ]
    }
  },
  {
  "name": "ApplicationInsightsAvailability",
  "id": "ApplicationInsightsAvailability",
  "properties": {
    "changeNumber": 2,
    "region": "",
    "regionId": 0,
    "platform": "Azure",
    "systemService": "ApplicationInsightsAvailability",
    "addressPrefixes": [
      "13.86.97.224/27",
      "13.86.98.0/27",
      "13.86.98.48/28",
      "13.86.98.64/28",
      "20.37.156.64/27",
      "20.37.192.80/29",
      "20.38.80.80/28",
      "20.40.104.96/27",
      "20.40.104.128/27",
      "20.40.124.176/28",
      "20.40.124.240/28",
      "20.40.125.80/28",
      "20.40.129.32/27",
      "20.40.129.64/26",
      "20.40.129.128/27",
      "20.42.4.64/27",
      "20.42.35.32/28",
      "20.42.35.64/26",
      "20.42.35.128/28",
      "20.42.129.32/27",
      "20.43.40.80/28",
      "20.43.64.80/29",
      "20.43.128.96/29",
      "20.45.5.160/27",
      "20.45.5.192/26",
      "20.189.106.64/29",
      "23.100.224.16/28",
      "23.100.224.32/27",
      "23.100.224.64/26"
    ],
    "networkFeatures": [
      "API",
      "NSG",
      "UDR",
      "FW"
    ]
  }
},
{
  "name": "AzureActiveDirectory",
  "id": "AzureActiveDirectory",
  "properties": {
    "changeNumber": 8,
    "region": "",
    "regionId": 0,
    "platform": "Azure",
    "systemService": "AzureAD",
    "addressPrefixes": [
      "13.64.151.161/32",
      "13.66.141.64/27",
      "13.67.9.224/27",
      "13.69.66.160/27",
      "13.69.229.96/27",
      "13.70.73.32/27"
    ],
    "networkFeatures": [
      "API",
      "NSG",
      "UDR",
      "FW",
      "VSE"
    ]
  }
}

Thank you for your time.

import urllib
import json
from urllib import urlopen


testfile = urllib.URLopener()
testfile.retrieve("https://download.microsoft.com/download/7/1/D/71D86715-5596-4529-9B13- 
DA13A5DE5B63/ServiceTags_Public_20210426.json", "AzureIPs.json")

print("---SUCCESSFULLY RECEIVED MICROSOFT AZURE IPS---")

with open('AzureIPs.json','r') as f:
     data = json.load(f)


#################  CHANGES AFTER THIS LINE  ################# 

    ips = []

    values = data['values']
    for block in values:
        ips.append(block.properties.addressPrefixes)

However you will get 2D array using this approach, if you need 1D array and not separate block of IPs from each corresponding block in values , you can use following code to flatten the array.

import numpy as np

2DArray = np.array(ips)
1DArray = 2DArray.flatten()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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