How to parse json file that content value/object via http url - python

I think my questions below different and im not able to find problem similar like mine...
I have json file (scanner.json) content values of nodes and links below
{
"nodes": [
"node1",
"node2",
"node3",
"node4",
"node5",
"node6",
"node7"
],
"links": [
[
"node1",
"node2",
"120"
],
[
"node1",
"node3",
"120"
],
[
"node1",
"node4",
"120"
],
[
"node2",
"node3",
"120"
],
[
"node2",
"node7",
"120"
]
]
}
I need to pass the values of nodes and links to NBI API below
Request URL: http://172.18.10.10:6233/cv/api/scanner
Body:
{
"channel": "scanner",
"action": "create_device",
"table": "U2",
"nodes::[
{
"node_hostname": "node1",
{,
{
"node_hostname": "node2",
},
{
"node_hostname": "node3"
}
"links": [
{
"source_hostname": "node1",
"dest_hostname": "node2",
"matric": "120",
},
]
}
How can I do that? parse and send via http url the content of the json file above to NBI API above.
Please assist and advise me. Thank you

Related

How to convert JSON to GeoJSON with Python

I'm actually learning how to do some cartography with python but first I would like to convert my json file to a GeoJson dynamically with Python. This is how my Json looks like :
[
{
"shape_name": "unit_shape",
"source_name": "7724C_BUSSY_SAINT_GEORGES_Bussycomore",
"building_id": "7724C",
"chaud_froid": "Chaud",
"geojson": {
"type": "LineString",
"properties": {
"densite_cc": null
},
"coordinates": [
[
2.726142,
48.834359
],
[
2.726149,
48.834367
],
[
2.726202,
48.834422
],
[
2.726262,
48.834429
],
[
2.726307,
48.834434
],
[
2.726316,
48.834435
]
]
},
"to_drop": null,
"id_enquete": null,
"shape_nom": null
},
...(many other features similar to the precedent one)]
Can someone please help me ?
import json
input_file=json.load(open("./data/data.json", "r", encoding="utf-8"))
geojs={
"type": "FeatureCollection",
"features":[
{
"type":"Feature",
"geometry": {
"type":"LineString",
"coordinates":d["geojson"]["coordinates"],
},
"properties":d,
} for d in input_file
]
}
output_file=open("./data/geodata.json", "w", encoding="utf-8")
json.dump(geojs, output_file)
output_file.close()

Adding/appending keys to JSON structure in python

Running Python 3.6 without supporting additional modules. I would like to append/add new entries to the "polygon" section. Any suggestions how:
JSON Structure:
{
"messageId":775,
"value":{
"dataFrames":[
{
"content":{
"workZone":[
{
"item":{
"text":"Test"
}
},
{
"item":{
"itis":333
}
}
]
},
"duratonTime":24,
"frameType":"road Signage",
"msgId":{
"roadSignID":{
"mutcdCode":"warning",
"position":{
"elevation":634.0,
"lat":30.2,
"long":-80.5
},
"viewAngle":"111111"
}
},
"priority":1,
"regions":[
{
"anchor":{
"elevation":634.0,
"lat":34.3,
"long":-80.5
},
"description":{
"geometry":{
"direction":"0000",
"laneWidth":5.123,
"polygon":[
[
]
]
}
},
"directionality":"forward"
}
],
"sspLocationRights":1,
"sspMsgRights1":0,
"sspMsgRights2":0,
"sspTimRights":1,
"startTime":1599041581.5259035,
"startYear":2020
}
],
"msgCnt":0,
"packetID":775,
"source":"XX"
}
}
I tried to form a JSON and adding a String for the polygon but it is being added with quotes. So not sure if the best way is to access the polygon section and add new entries.
Expected JSON:
{
"messageId":775,
"value":{
"msgCnt":0,
"packetID":775,
"source":"C-V2X",
"dataFrames":[
{
"sspTimRights":1,
"frameType":"road Signage",
"msgId":{
"roadSignID":{
"position":{
"lat":-80.38466909433639,
"long":37.17942971412366,
"elevation":634.0
},
"viewAngle":"1000000000000000",
"mutcdCode":"warning"
}
},
"startYear":2020,
"startTime":1598992048.1489706,
"duratonTime":24,
"priority":1,
"sspLocationRights":1,
"regions":[
{
"anchor":{
"lat":-80.38466909433639,
"long":37.17942971412366,
"elevation":634.0
},
"directionality":"forward",
"description":{
"geometry":{
"direction":"1000000000000000",
"laneWidth":5.123,
"polygon":[
[
[
37.17942971412366,
-80.38466909433639
],
[
37.179543821887314,
-80.38487318094833
],
[
37.17967679727881,
-80.38510713731363
],
[
37.17995588265411,
-80.38560355518067
],
[
37.17998272884397,
-80.38557977915941
],
[
37.179703594552834,
-80.38508327461031
],
[
37.17957064376986,
-80.38484936187977
],
[
37.17945660930624,
-80.38464540586482
],
[
37.17942971412366,
-80.38466909433639
]
]
]
}
}
}
],
"sspMsgRights1":0,
"sspMsgRights2":0,
"content":{
"workZone":[
{
"item":{
"text":"Buffer Area"
}
},
{
"item":{
"itis":775
}
}
]
}
}
]
}
}
As #Gledi suggested. Doing a List is the proper way to insert the "polygon" information into the JSON structure.
Assuming data points to the dict in the question - the code below should work.
data['value']['dataFrames'][0]['regions'][0]['description']['geometry']['polygon'][0].append('something')

Sending an HTTP API Request in Python and get json data to csv format

I have HTTP URL and API request body, i need to post the request using python and get output in json and convert into csv file.
As i am new to this python, couldn't succeed. Can any one please help me on this.
URL: as below
http://{{WEBURL}}:{{WEBPORT}}/{{TENANT_ID}}/api/requesttrackingservice/get
API body as below
{
"params": {
"query": {
"filters": {
"typesCriterion": [
"tasksummaryobject"
],
"propertiesCriterion": [
{
"modifiedDate": {
"gte": "2019-08-26T06:00:00.000-0500",
"lte": "2019-08-30T19:00:00.000-0500",
"type": "_DATETIME"
}
}
],
"attributesCriterion": [
{
"connectIntegrationType": {
"eq": "ENTITY_IMPORT"
}
},
{
"profileName": {
"eq": "sys_import_data_json_eventhub_task_base"
}
}
]
}
},
"fields": {
"attributes": [
"status","totalRecordsProcessed","totalRecordsCreate","totalRecordsupdate","totalRecordsDelete","filetype"
],
"relationships": [
"_ALL"
]
},
"options": {
"maxRecords": 1000
}
}
}
Assigning your url to a URL variable, and your json to a JSON variable, you could try:
import requests
response = requests.post(url=URL, json=JSON)
print(response.content)

Update a json file from XLS file data

Im trying to update a json file from XLS file data.
This what I wish to do :
Extract namesFromJson
Extract namesFromXLS
for nameFromXLS in namesFromXLS :
Check if nameFromXLS is in namesFromJson :
if true : then :
extract xls row (of this name)
update jsonFile (of this name)
My problem is when its true, how can I update a jsonfile?
Python code:
import xlrd
import unicodedata
import json
intents_file = open("C:\myJsonFile.json","rU")
json_intents_data = json.load(intents_file)
book = xlrd.open_workbook("C:\myXLSFile.xlsx")
sheet = book.sheet_by_index(0)
row =""
nameXlsValues = []
intentJsonNames =[]
for entity in json_intents_data["intents"]:
intentJsonName = entity["name"]
intentJsonNames.append(intentJsonName)
for row_index in xrange(sheet.nrows):
nameXlsValue = sheet.cell(rowx = row_index,colx=0).value
nameXlsValues.append(nameXlsValue)
if nameXlsValue in intentJsonNames:
#here ,I have to extract row values from xlsFile and update jsonFile
for col_index in xrange(sheet.ncols):
value = sheet.cell(rowx = row_index,colx=col_index).value
if type(value) is unicode:
value = unicodedata.normalize('NFKD',value).encode('ascii','ignore')
row += "{0} - ".format(value)
my json file is like this :
{
"intents": [
{
"id": "id1",
"name": "name1",
"details": {
"tags": [
"tag1"
],
"answers": [
{
"type": "switch",
"cases": [
{
"case": "case1",
"answers": [
{
"tips": [
""
],
"texts": [
"my text to be updated"
]
}
]
},
{
"case": "case2",
"answers": [
{
"tips": [
"tip2"
],
"texts": [
]
}
]
}
]
}
],
"template": "json",
"sentences": [
"sentence1",
" sentence2",
" sentence44"]
}
},
{
"id": "id2",
"name": "name3",
"details": {
"tags": [
"tag2"
],
"answers": [
{
"type": "switch",
"cases": [
{
"case": "case1",
"answers": [
{
"texts": [
""
]
}
]
},
{
"case": "case2",
"answers": [
{
"texts": [
""
]
}
]
}
]
}
],
"sentences": [
"sentence44",
"sentence2"
]
}
}
]
}
My xls file is like this :
[![enter image description here][1]][1]
When you are loading the json data from file into memory it becomes a python dict named 'json_intents_data'.
When the condition 'if nameXlsValue in intentJsonNames' is True you need to update the dict with the data you read from the Excel. (It looks like you know how to do it.)
When the loop 'for row_index in xrange(sheet.nrows):' is done, your dict is updated and you want to save it as a json file.
import json
with open('updated_data.json', 'w') as fp:
json.dump(json_intents_data, fp)

How do I add an array of RecordSets into Cloud Formation using troposphere?

I'm using the python module troposphere to create my cloud formation template. Most of it is complete but I seem to be confused about how to create my DNS entries for the load balancer with the RecordSets method/function. The output for this section is supposed to look like:
"devdevopsdemoELBDNSARecord0": {
"Type": "AWS::Route53::RecordSetGroup",
"Properties": {
"HostedZoneName": "FOO.net.",
"Comment": "Alias targeted to devdevopsdemoELB ELB.",
"RecordSets": [
{
"Name": "devopsdemo.dev.FOO.net.",
"Type": "A",
"AliasTarget": {
"HostedZoneId": {
"Fn::GetAtt": [
"devdevopsdemoELB",
"CanonicalHostedZoneNameID"
]
},
"DNSName": {
"Fn::GetAtt": [
"devdevopsdemoELB",
"CanonicalHostedZoneName"
]
}
}
},
{
"Name": "devopsdemo-dev.FOO.net.",
"Type": "A",
"AliasTarget": {
"HostedZoneId": {
"Fn::GetAtt": [
"devdevopsdemoELB",
"CanonicalHostedZoneNameID"
]
},
"DNSName": {
"Fn::GetAtt": [
"devdevopsdemoELB",
"CanonicalHostedZoneName"
]
}
}
}
I've started with:
hostedzone = "FOO.net"
myRecordSet = RecordSetType("devdevopsdemoELBDNSARecord0")
myRecordSet.HostedZoneName=Join("", hostedzone, "."])
myRecordSet.Comment="Alias targeted to devdevopsdemoELB ELB."
But then I'm not clear on how the RecordSets values should be entered.
I supposed I could just use the straight
myRecordSet.RecordSets =
And just put the json into place, but that seems a bit like a misuse of the purpose of using troposphere in the first place.
Update: Putting in the json results in this error
AttributeError: AWS::Route53::RecordSet object does not support attribute RecordSets
myRecordSet.RecordSets = [
{
"Name": "devopsdemo.dev.FOO.net.",
"Type": "A",
"AliasTarget": {
"HostedZoneId": {
"Fn::GetAtt": [
"devdevopsdemoELB",
"CanonicalHostedZoneNameID"
]
},
"DNSName": {
"Fn::GetAtt": [
"devdevopsdemoELB",
"CanonicalHostedZoneName"
]
}
}
},
{
"Name": "devopsdemo-dev.FOO.net.",
"Type": "A",
"AliasTarget": {
"HostedZoneId": {
"Fn::GetAtt": [
"devdevopsdemoELB",
"CanonicalHostedZoneNameID"
]
},
"DNSName": {
"Fn::GetAtt": [
"devdevopsdemoELB",
"CanonicalHostedZoneName"
]
}
}
}
]
Then I tried this:
myDNSRecord = t.add_resource(RecordSetGroup(
"devdevopsdemoELBDNSARecord0",
HostedZoneName=Join("", ["inpwrd.net", "."]),
Comment="DNS Entry to point to the ELB for devopsdemo",
RecordSets=[
RecordSet(
Name="devopsdemo.dev.inpwrd.net.",
Type="A",
AliasTarget=[HostedZoneId(GetAtt("devdevopsdemoELB", "CanonicalHostedZoneNameID")),
DNSName(GetAtt("devdevopsdemoELB", "CanonicalHostedZoneName"))],
),
RecordSet(
Name="devopsdemo-dev.inpwrd.net.",
Type="A",
AliasTarget=[HostedZoneId(GetAtt("devdevopsdemoELB", "CanonicalHostedZoneNameID")),
DNSName(GetAtt("devdevopsdemoELB", "CanonicalHostedZoneName"))],
),
],
)
)
HostedZoneId isn't found
AliasTarget takes an AliasTarget helper class so you want to do it this way:
from troposphere import GetAtt, Join, Template
from troposphere.route53 import AliasTarget, RecordSetType, RecordSetGroup, RecordSet
t = Template()
myDNSRecord = t.add_resource(RecordSetGroup(
"devdevopsdemoELBDNSARecord0",
HostedZoneName=Join("", ["example.net", "."]),
Comment="DNS Entry to point to the ELB for devopsdemo",
RecordSets=[
RecordSet(
Name="devopsdemo.dev.example.net.",
Type="A",
AliasTarget=AliasTarget(
GetAtt("devdevopsdemoELB", "CanonicalHostedZoneNameID"),
GetAtt("devdevopsdemoELB", "CanonicalHostedZoneName"),
),
),
RecordSet(
Name="devopsdemo-dev.example.net.",
Type="A",
AliasTarget=AliasTarget(
GetAtt("devdevopsdemoELB", "CanonicalHostedZoneNameID"),
GetAtt("devdevopsdemoELB", "CanonicalHostedZoneName"),
),
),
],
)
)
print t.to_json()

Categories

Resources