Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

The following python example will read all the location info for a given filterId PI_JSON locations webservice call and write it to a locations.csv and attributes.csv. The script has been tested using python 3.7 and requires the requests, csv and json modules:

Code Block
languagepy
import requests
import json
import csv

url = 'http://localhost:8080/FewsWebServices/rest/fewspiservice/v1/locations?showAttributes=true'
params = dict(
    documentVersion='1.2425',
    documentFormat='PI_JSON'
)
response = requests.get(url=url, params=params)
data = response.json()  # alle data in JSON (dictionary) formaat
locations = data['locations'] # haalt alle parameters uit de dictionary

# collect all possibile location keys in a list
location_keys_list = []
for location in locations:
    keys = location.keys()
    for key in keys:
        if key not in location_keys_list:
            location_keys_list.append(key)
# remove 'attributes' element as these will be handled seperately
if 'attributes' in location_keys_list: location_keys_list.remove('attributes')

# collect all possibile attributes in a list
attribute_list = []
for location in locations:
    for attribute in location['attributes']:
        attr_id = str(attribute.get('id'))
        if attr_id not in attribute_list:
            attribute_list.append(attr_id)
# insert locationId as the first element in the attribute list
attribute_list.insert(0,'locationId')
            
with open('locations.csv', 'w', newline='', encoding='utf-8') as loc_csv:
    l = csv.writer(loc_csv, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
    l.writerow(location_keys_list)

    for location in locations:
        temp_dict = {}
        for key in location_keys_list:
            if key in location:
                temp_dict[key] = str(location[key])
            else:
                temp_dict[key] = ''
        loc_writerow = [temp_dict[k] for k in location_keys_list]
        l.writerow(loc_writerow)

with open('attributes.csv', 'w', newline='', encoding='utf-8') as attr_csv:
    a = csv.writer(attr_csv, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
    a.writerow(attribute_list)

    for location in locations:
        temp_dict = {}
        for attribute in location['attributes']:
            for attr in attribute_list:
                if str(attribute.get('id')) == attr:
                    if attribute.get('type') == 'text':
                        temp_dict[attr] = str(attribute.get('value'))
                    if attribute.get('type') == 'number':
                        temp_dict[attr] = float(attribute.get('value')) 
                    if attribute.get('type') == 'boolean':
                        temp_dict[attr] = bool(attribute.get('value')) 
        for attr in attribute_list:
            if not attr in temp_dict.keys():
                temp_dict[attr] = ''
        temp_dict['locationId'] = str(location['locationId'])
        attr_writerow = [temp_dict[a] for a in attribute_list]
        a.writerow(attr_writerow)
        
print('Finished!')

...