When importing JSON it says there is an error that it has extra character - python-3.x

When I import a JSON from a URL it says it has an extra character. Below is the:
Code:
import requests
import json
response = json.loads(requests.get("https://s3-us-west-2.amazonaws.com/anand-fhir-json/2e3b7aa0-0c90-4b31-8967-680f1438e03e").text)
print(res1);
JSON format:
{"id":{"s":"2"},"managingOrganization":{"s":"{'reference': 'Organization/hl7'}"},"address":{"s":"[{'use': 'home', 'line': ['2222 Home Street']}]"},"name":{"s":"[{'use': 'official', 'family': 'Everyman', 'given': ['Adam']}]"},"telecom":{"s":"[{'system': 'phone', 'value': '555-555-2004', 'use': 'work'}]"},"gender":{"s":"male"},"active":{"s":"true"},"birthDate":{"s":"null"},"meta":{"s":"{'lastUpdated': '2012-05-29T23:45:32Z'}"},"resourceType":{"s":"Patient"}}
{"id":{"s":"8"},"managingOrganization":{"s":"{'reference': 'Organization/hl7'}"},"address":{"s":"[{'use': 'home', 'line': ['4444 Home Street']}]"},"name":{"s":"[{'use': 'official', 'family': 'Mum', 'given': ['Martha']}]"},"telecom":{"s":"[{'system': 'phone', 'value': '555-555-2006', 'use': 'work'}]"},"gender":{"s":"female"},"active":{"s":"true"},"birthDate":{"s":"null"},"meta":{"s":"{'lastUpdated': '2012-05-29T23:45:32Z'}"},"resourceType":{"s":"Patient"}}

Related

Python Altair, query for current axis limits

I know how to set axis limits and whatnot, but how do I query for currently used axis limits?
I don't think this is possible unless you specifically set an axis limit first:
import altair as alt
from vega_datasets import data
source = data.cars.url
chart = alt.Chart(source).mark_circle().encode(
x='Horsepower:Q',
y='Miles_per_Gallon:Q',
)
chart.to_dict()
{'config': {'view': {'continuousWidth': 400, 'continuousHeight': 300}},
'data': {'url': 'https://cdn.jsdelivr.net/npm/vega-datasets#v1.29.0/data/cars.json'},
'mark': 'circle',
'encoding': {'x': {'field': 'Horsepower', 'type': 'quantitative'},
'y': {'field': 'Miles_per_Gallon', 'type': 'quantitative'}},
'$schema': 'https://vega.github.io/schema/vega-lite/v5.2.0.json'}
If you set the domain, you can see it in the spec:
chart = alt.Chart(source).mark_circle().encode(
x=alt.X('Horsepower:Q', scale=alt.Scale(domain=[0, 250])),
y='Miles_per_Gallon:Q',
)
chart.to_dict()
{'config': {'view': {'continuousWidth': 400, 'continuousHeight': 300}},
'data': {'url': 'https://cdn.jsdelivr.net/npm/vega-datasets#v1.29.0/data/cars.json'},
'mark': 'circle',
'encoding': {'x': {'field': 'Horsepower',
'scale': {'domain': [0, 250]},
'type': 'quantitative'},
'y': {'field': 'Miles_per_Gallon', 'type': 'quantitative'}},
'$schema': 'https://vega.github.io/schema/vega-lite/v5.2.0.json'}
and get it via chart.to_dict()['encoding']['x']['scale']['domain'].

Forming a list of dicts in a loop according to if conditions

I need your help.
I have this code:
import ipaddress
from ipaddress import IPv4Network
prefixes = []
ip_addresses_all = [{'address': '10.0.0.1/24', 'vrf': {'id': 31,'name': 'god_inet'},
{'address': '10.0.0.10/24', 'vrf': {'id': 33, 'name': 'for_test'},
{'address': '10.1.1.1/30', 'vrf': {'id': 8, 'name': 'ott_private_net'},
{'address': '10.1.1.2/30', 'vrf': {'id': 11,'name': 'ott_public_net'},
{'address': '10.10.0.129/30', 'vrf': None,},
{'address': '10.10.0.130/30', 'vrf': None,},
{'address': '10.10.0.137/30', 'vrf': None,},
{'address': '10.10.0.138/30', 'vrf': None,}]
for ip in ip_addresses_all:
prefix = str(ipaddress.ip_network(ip.address, False))
mask_length = int(IPv4Network(prefix).prefixlen)
description_interface = ip.address
if ip.vrf:
ip_vrf_name = ip.vrf.name
ip_vrf_id = ip.vrf.id
ip_vrf = ip.vrf
else:
ip_vrf_name = 'null'
ip_vrf_id = 'null'
ip_vrf = 'null'
prefix_dict = {'prefix': prefix,
'vrf': {'name': ip_vrf_name,
'id': ip_vrf_id},
'prefix_description': [description_interface]}
if prefixes:
for i in prefixes:
if prefix != i['prefix']:
prefixes.append(prefix_dict)
elif i['prefix'] == prefix and i['vrf']['name'] == ip_vrf_name and description_interface not in i['prefix_description']:
i['prefix_description'].append(description_interface)
else:
prefixes.append(prefix_dict)
pprint(prefixes)
So, I wanna append the 'prefixes' list with dicts according to this logic : if prefix not in prefixes = [] - create new dict , if prefix with the same vrf already exists, then append a description string to a description key and update existing dict.
I struggle with this for 8 hours and it doesnt work, Ive got infinite loops=)
Intended output, something like that:
{'10.0.0.0/24': {'prefix_description': ['10.0.0.10/24'],'vrf': {'id': 31, 'name': 'god_inet'}},
'10.0.0.0/24': {'prefix_description': ['10.0.0.10/24'],'vrf': {'id': 33, 'name': 'for_test'}},
'10.1.1.0/30': {'prefix_description': ['10.1.1.2/30'],'vrf': {'id': 8, 'name': 'ott_private_net'}},
'10.1.1.0/30': {'prefix_description': ['10.1.1.2/30'],'vrf': {'id': 11, 'name': 'ott_public_net'}},
'10.10.0.128/30': {'prefix_description': ['10.10.0.129/30',
'10.10.0.130/30'],'vrf': {'id': 'null', 'name': 'null'}},
'10.10.0.136/30': {'prefix_description': ['10.10.0.137/30',
'10.10.0.138/30'],'vrf': {'id': 'null', 'name': 'null'}}}
Or better in a list like that:
[{'prefix': '10.0.0.0/24',
'prefix_description': ['77-GOD-VPN-2 ---- Vlan40'],
'vrf': {'id': 31, 'name': 'god_inet'}},
{'prefix': '10.0.0.0/24',
'prefix_description': ['78-ELS-CORE ---- Vlan142'],
'vrf': {'id': 33, 'name': 'for_test'}}]
make prefixes a dict and handle that code in the for loop
import ipaddress
from ipaddress import IPv4Network
prefixes = {}
ip_addresses_all = [{'address': '10.0.0.1/24', 'vrf': {'id': 31,'name': 'god_inet'}},
{'address': '10.0.0.10/24', 'vrf': {'id': 33, 'name': 'for_test'}},
{'address': '10.1.1.1/30', 'vrf': {'id': 8, 'name': 'ott_private_net'}},
{'address': '10.1.1.2/30', 'vrf': {'id': 11,'name': 'ott_public_net'}},
{'address': '10.10.0.129/30', 'vrf': None,},
{'address': '10.10.0.130/30', 'vrf': None,},
{'address': '10.10.0.137/30', 'vrf': None,},
{'address': '10.10.0.138/30', 'vrf': None,}]
for ip in ip_addresses_all:
prefix = str(ipaddress.ip_network(ip['address'], False))
mask_length = int(IPv4Network(prefix).prefixlen)
description_interface = ip['address']
if ip['vrf']:
ip_vrf_name = ip['vrf']['name']
ip_vrf_id = ip['vrf']['id']
ip_vrf = ip['vrf']
else:
ip_vrf_name = 'null'
ip_vrf_id = 'null'
ip_vrf = 'null'
prefix_dict = {
'vrf': {'name': ip_vrf_name,
'id': ip_vrf_id},
'prefix_description': [description_interface]}
if prefix in prefixes and \
prefixes[prefix]['vrf']['name'] == ip_vrf_name and \
description_interface not in prefixes[prefix]['prefix_description']:
prefixes[prefix]['prefix_description'].append(description_interface)
else:
prefixes[prefix] = prefix_dict
from pprint import pprint
pprint(prefixes)
output
{'10.0.0.0/24': {'prefix_description': ['10.0.0.10/24'],
'vrf': {'id': 33, 'name': 'for_test'}},
'10.1.1.0/30': {'prefix_description': ['10.1.1.2/30'],
'vrf': {'id': 11, 'name': 'ott_public_net'}},
'10.10.0.128/30': {'prefix_description': ['10.10.0.129/30', '10.10.0.130/30'],
'vrf': {'id': 'null', 'name': 'null'}},
'10.10.0.136/30': {'prefix_description': ['10.10.0.137/30', '10.10.0.138/30'],
'vrf': {'id': 'null', 'name': 'null'}}}

Change Icon color in a TimestampedGeoJson

I am trying to make a map including popups with iframes markers with different colors and icons with the data included in a geojson . I have no problem with the popups, but I can't change the icon colors.
I have tried the following ways and other ones that I can't remember to introduce the color in 'properties' without any result:
{'type': 'Feature', 'properties': {'icon': {'color': 'red'},...}
{'type': 'Feature', 'properties': {'color': 'red',...}
{'type': 'Feature', 'properties': {'style': {'color': 'red'},...}
{'type': 'Feature', 'properties': {'icon': 'marker', 'color': 'red',...}
Here is my code:
import folium
from folium.plugins import TimestampedGeoJson
m=folium.Map(location=[46, 20.00],
zoom_start=7,
tiles="stamenterrain")
features = [{'type': 'Feature',
'properties': {'color': 'green',
'name': 'Photo 1',
'time': '2012-01-01',
'popup': "<iframe src=https://images.pexels.com/photos/96491/pexels-photo-96491.jpeg title='test' width='200' height='100'/>"},
'geometry': {'type': 'Point', 'coordinates': [19.295, 46.5116]}},
{'type': 'Feature',
'properties': {'color': 'red',
'name': 'Photo 2',
'time': '2012-04-01',
'popup': "<iframe src=https://images.pexels.com/photos/1145370/pexels-photo-1145370.jpeg title='test' width='200' height='100'/>"},
'geometry': {'type': 'Point', 'coordinates': [18.15291, 47.20544]}}]
TimestampedGeoJson(
{'type': 'FeatureCollection', 'features': features},
period='P3M',
duration='P2M',
auto_play=False,
add_last_point=True,
min_speed=0.5,
max_speed=2,
loop=False,
loop_button=True,
date_options='YYYY/MM/DD',
).add_to(m)
m
I expect to obtain a map with green and red colors, but markers are showed in blue.
If I use circle icon using 'properties': {'icon': 'circle', 'iconstyle': {'color': 'green'} the circles are created with teh correct color.
Replace color with iconColor like this:
'properties': {'icon': 'circle', 'iconstyle': {'iconColor': 'green'}

Python nested json to csv

I can't convert this Json to csv. I have been trying with different solutions posted here using panda or other parser but non solved this.
This is a small extract of the big json
{'data': {'items': [{'category': 'cat',
'coupon_code': 'cupon 1',
'coupon_name': '$829.99/€705.79 ',
'coupon_url': 'link3',
'end_time': '2017-12-31 00:00:00',
'language': 'sp',
'start_time': '2017-12-19 00:00:00'},
{'category': 'LED ',
'coupon_code': 'code',
'coupon_name': 'text',
'coupon_url': 'link',
'end_time': '2018-01-31 00:00:00',
'language': 'sp',
'start_time': '2017-10-07 00:00:00'}],
'total_pages': 1,
'total_results': 137},
'error_no': 0,
'msg': '',
'request': 'GET api/ #2017-12-26 04:50:02'}
I'd like to get an output like this with the columns:
category, coupon_code, coupon_name, coupon_url, end_time, language, start_time
I'm running python 3.6 with no restrictions.

Export nested dictionary to csv

I need to export a nested dictionary to CSV. Here's what each entry looks like (that needs to be one line in the csv later):
{'createdTime': '2017-10-30T12:33:02.000Z',
'fields': {'Date': '2017-10-30T12:32:56.000Z',
'field1': 'example#gmail.com',
'field2': 1474538185964188,
'field3': 6337,
....},
'id': 'reca7LBr64XM1ClWy'}
I think I need to iterate through the dictionary and create a list of lists(?) to create the csv from using the csv module.
['Date', 'field1', 'field2', 'field3', ...],
['2017-10-30T12:32:56.000Z', 'example#gmail.com', 1474538185964188, 6337 ...]
My problem is to find a smart way to iterate through the dict to get to a list like this.
You can get the values in the below way:
def process_data():
csv_data = [{'createdTime': '2017-10-30T12:33:02.000Z',
'fields': {'Date': '2017-10-30T12:32:56.000Z',
'field1': 'example#gmail.com',
'field2': 1474538185964188,
'field3': 6337},
'id': 'reca7LBr64XM1ClWy'},
{'createdTime': '2017-10-30T12:33:02.000Z',
'fields': {'Date': '2017-10-30T12:32:56.000Z',
'field1': 'example#gmail.com',
'field2': 1474538185964188,
'field3': 6337},
'id': 'reca7LBr64XM1ClWy'}]
headers = [key for key in csv_data[0]['fields'].keys()]
body = []
for row in csv_data:
body_row = []
for colomn_header in headers:
body_row.append(row['fields'][colomn_header])
body.append(body_row)
process_data()
#header -- ['Date', 'field1', 'field2', 'field3']
#body -- [['2017-10-30T12:32:56.000Z', 'example#gmail.com',
# 1474538185964188, 6337], ['2017-10-30T12:32:56.000Z',
# 'example#gmail.com', 1474538185964188, 6337]]

Resources