Related
I am using the following code to update sales order on unleashed but it's not working, It's returning internal server error. It's working for get request.
import requests.auth
import json
import requests
import binascii
import hashlib
import hmac
unleashed_api_app_id = 'xxxxxxx'
unleashed_api_app_key = 'xxxxxx'
class UnleashedAuth(requests.auth.AuthBase):
def __init__(self, api_id, api_key):
self.api_id = api_id
self.api_key = api_key
def get_query(self, url):
parts = url.split('?')
if len(parts) > 1:
return parts[1]
else:
return ""
def __call__(self, r):
query = self.get_query(r.url)
hashed = hmac.new(self.api_key, query, hashlib.sha256)
signature = binascii.b2a_base64(hashed.digest())[:-1]
r.headers['api-auth-signature'] = signature
r.headers['api-auth-id'] = self.api_id
return r
a = UnleashedAuth(unleashed_api_app_id, unleashed_api_app_key)
h = {
'content-type': 'application/json',
'accept': 'application/json',
}
i = {
"SalesOrderLines": [
{
"LineNumber": 1,
"LineType": None,
"Product": {
"Guid": "00000000-0000-0000-0000-000000000000",
"ProductCode": "SHELF",
"ProductDescription": "Bookshelf Shelf"
},
"DueDate": "\/Date(1597190400000)\/",
"OrderQuantity": 10,
"UnitPrice": 120,
"DiscountRate": 0.02,
"LineTotal": 1176,
"Volume": None,
"Weight": None,
"Comments": "",
"AverageLandedPriceAtTimeOfSale": 86.3,
"TaxRate": 0,
"LineTax": 0,
"XeroTaxCode": "NONE",
"BCUnitPrice": 117.6,
"BCLineTotal": 1176,
"BCLineTax": 0,
"LineTaxCode": None,
"XeroSalesAccount": None,
"SerialNumbers": None,
"BatchNumbers": None,
"Guid": "00000000-0000-0000-0000-000000000000",
"LastModifiedOn": "\/Date(1597142565050)\/"
},
{
"LineNumber": 0,
"LineType": "Charge",
"Product": {
"Guid": "00000000-0000-0000-0000-000000000000",
"ProductCode": None,
"ProductDescription": "10"
},
"DueDate": "\/Date(1597190400000)\/",
"OrderQuantity": 1,
"UnitPrice": 20,
"DiscountRate": 0,
"LineTotal": 20,
"Volume": None,
"Weight": None,
"Comments": None,
"AverageLandedPriceAtTimeOfSale": None,
"TaxRate": 0,
"LineTax": 0,
"XeroTaxCode": None,
"BCUnitPrice": 20,
"BCLineTotal": 20,
"BCLineTax": 0,
"LineTaxCode": "NONE",
"XeroSalesAccount": None,
"SerialNumbers": None,
"BatchNumbers": None,
"Guid": "00000000-0000-0000-0000-000000000000",
"LastModifiedOn": None
}
],
"OrderNumber": "xxxxxxxx",
"OrderDate": "\/Date(1597104000000)\/",
"RequiredDate": "\/Date(1597190400000)\/",
"CompletedDate": None,
"OrderStatus": "Completed",
"Customer": {
"CustomerCode": "PTECH",
"CustomerName": "PivacTech",
"CurrencyId": 48,
"Guid": "00000000-0000-0000-0000-000000000000",
"LastModifiedOn": "\/Date(1597142415046)\/"
},
"CustomerRef": None,
"Comments": None,
"Warehouse": {
"WarehouseCode": "MAIN",
"WarehouseName": "Main Warehouse",
"IsDefault": True,
"StreetNo": None,
"AddressLine1": None,
"AddressLine2": None,
"Suburb": None,
"City": None,
"Region": None,
"Country": None,
"PostCode": None,
"PhoneNumber": None,
"FaxNumber": None,
"MobileNumber": None,
"DDINumber": None,
"ContactName": None,
"Obsolete": False,
"Guid": "00000000-0000-0000-0000-000000000000",
"LastModifiedOn": "\/Date(1597121276596)\/"
},
"ReceivedDate": None,
"DeliveryInstruction": None,
"DeliveryName": "Test",
"DeliveryStreetAddress": "#test house",
"DeliveryStreetAddress2": None,
"DeliverySuburb": "Test ",
"DeliveryCity": "Test",
"DeliveryRegion": "Test",
"DeliveryCountry": "India",
"DeliveryPostCode": None,
"Currency": {
"CurrencyCode": "GBP",
"Description": "United Kingdom, Pounds",
"Guid": "00000000-0000-0000-0000-000000000000",
"LastModifiedOn": "\/Date(1597012885460)\/"
},
"ExchangeRate": 1,
"DiscountRate": 0.02,
"Tax": {
"TaxCode": "NONE",
"Description": None,
"TaxRate": 0,
"CanApplyToExpenses": False,
"CanApplyToRevenue": False,
"Obsolete": False,
"Guid": "00000000-0000-0000-0000-000000000000",
"LastModifiedOn": None
},
"TaxRate": 0,
"XeroTaxCode": "NONE",
"SubTotal": 1196,
"TaxTotal": 0,
"Total": 1196,
"TotalVolume": 0,
"TotalWeight": 0,
"BCSubTotal": 1196,
"BCTaxTotal": 0,
"BCTotal": 1196,
"PaymentDueDate": "\/Date(1600560000000)\/",
"AllocateProduct": True,
"SalesOrderGroup": "Website",
"DeliveryMethod": "Delivery",
"SalesPerson": {
"FullName": "test name",
"Email": "test email",
"Obsolete": False,
"Guid": "00000000-0000-0000-0000-000000000000",
"LastModifiedOn": "\/Date(1534049276566)\/"
},
"SendAccountingJournalOnly": False,
"SourceId": None,
"CreatedBy": "test email",
"CreatedOn": "\/Date(1597122197737)\/",
"LastModifiedBy": "test email",
"Guid": "00000000-0000-0000-0000-000000000000",
"LastModifiedOn": "\/Date(1597142565062)\/"
}
guid = "00000000-0000-0000-0000-000000000000"
unleashed_api_url = 'https://api.unleashedsoftware.com/SalesOrders/'+str(guid)
resp = requests.put(
unleashed_api_url,
i,
auth=a,
headers=h,
)
print("resp2===", resp.text)
Following is output it returns:
('resp2===', u'{"Description":"There was an internal server error.","DebugInformation":null}')
The same code is working using their sandbox but not working with my local request.
The problem is when I am running the script I am getting the following error:
Traceback (most recent call last): File "test1.py", line 16, in
output.writerow(data[0].keys()) KeyError: 0
Following is the python code:
import csv, json, sys
if sys.argv[1] is not None and sys.argv[2] is not None:
fileInput = sys.argv[1]
fileOutput = sys.argv[2]
inputFile = open(fileInput)
outputFile = open(fileOutput, 'w')
data = json.load(inputFile)
inputFile.close()
output = csv.writer(outputFile)
output.writerow(data[0].keys())
for row in data:
output.writerow(row.values())
Formatted JSON data :
{
"response": [
{
"thread": "7046384220",
"numReports": 0,
"editableUntil": "2018-11-22T11:20:37",
"sb": false,
"media": [],
"moderationLabels": [],
"isSpam": false,
"isHighlighted": false,
"dislikes": 0,
"points": 1,
"raw_message": "A sinking pound, resignations by the bucketful railroading through a so-called agreement nobody wants. For the good of the country Mrs. May 'go and go now'.",
"forum": "expressandstar",
"createdAt": "2018-11-15T11:20:37",
"canVote": false,
"author": {
"profileUrl": "https://disqus.com/by/mnaid-233509073ed3432027d48b1a83f5fbd2/",
"isPrivate": false,
"isPrimary": true,
"about": "",
"username": "mnaid-233509073ed3432027d48b1a83f5fbd2",
"url": "",
"id": "250728493",
"isAnonymous": false,
"avatar": {
"permalink": "https://disqus.com/api/users/avatars/mnaid-233509073ed3432027d48b1a83f5fbd2.jpg",
"cache": "//a.disquscdn.com/1541535443/images/noavatar92.png",
"small": {
"permalink": "https://disqus.com/api/users/avatars/mnaid-233509073ed3432027d48b1a83f5fbd2.jpg",
"cache": "//a.disquscdn.com/1541535443/images/noavatar32.png"
},
"large": {
"permalink": "https://disqus.com/api/users/avatars/mnaid-233509073ed3432027d48b1a83f5fbd2.jpg",
"cache": "//a.disquscdn.com/1541535443/images/noavatar92.png"
},
"isCustom": false
},
"location": "",
"isPowerContributor": false,
"signedUrl": "",
"joinedAt": "2017-05-03T10:40:52",
"disable3rdPartyTrackers": true,
"name": "baggiebuoy"
},
"parent": null,
"isFlagged": false,
"likes": 1,
"id": "4196155749",
"isDeletedByAuthor": false,
"isDeleted": false,
"message": "<p>A sinking pound, resignations by the bucketful railroading through a so-called agreement nobody wants. For the good of the country Mrs. May 'go and go now'.</p>",
"isEdited": true,
"isApproved": true
},
{
"thread": "7046384220",
"numReports": 0,
"editableUntil": "2018-11-22T10:37:59",
"sb": false,
"media": [],
"moderationLabels": [],
"isSpam": false,
"isHighlighted": false,
"dislikes": 0,
"points": 0,
"raw_message": "This could be heaven, or this could be hell.",
"forum": "expressandstar",
"createdAt": "2018-11-15T10:37:59",
"canVote": false,
"author": {
"profileUrl": "https://disqus.com/by/mnaid-29e1c59be16c852670e3be302e8c303b/",
"isPrivate": false,
"isPrimary": true,
"about": "",
"username": "mnaid-29e1c59be16c852670e3be302e8c303b",
"url": "",
"id": "250739494",
"isAnonymous": false,
"avatar": {
"permalink": "https://disqus.com/api/users/avatars/mnaid-29e1c59be16c852670e3be302e8c303b.jpg",
"cache": "//a.disquscdn.com/1541535443/images/noavatar92.png",
"small": {
"permalink": "https://disqus.com/api/users/avatars/mnaid-29e1c59be16c852670e3be302e8c303b.jpg",
"cache": "//a.disquscdn.com/1541535443/images/noavatar32.png"
},
"large": {
"permalink": "https://disqus.com/api/users/avatars/mnaid-29e1c59be16c852670e3be302e8c303b.jpg",
"cache": "//a.disquscdn.com/1541535443/images/noavatar92.png"
},
"isCustom": false
},
"location": "",
"isPowerContributor": false,
"signedUrl": "",
"joinedAt": "2017-05-03T13:01:02",
"disable3rdPartyTrackers": false,
"name": "Mordecai"
},
"parent": 4196013534,
"isFlagged": false,
"likes": 0,
"id": "4196048572",
"isDeletedByAuthor": false,
"isDeleted": false,
"message": "<p>This could be heaven, or this could be hell.</p>",
"isEdited": false,
"isApproved": true
},
{
"thread": "7046384220",
"numReports": 0,
"editableUntil": "2018-11-22T10:36:50",
"sb": false,
"media": [],
"moderationLabels": [],
"isSpam": false,
"isHighlighted": false,
"dislikes": 0,
"points": 0,
"raw_message": "The crappest of crap deals.\nWill never get through Parliament.\n\nYou are the weakest link May, goodbye.",
"forum": "expressandstar",
"createdAt": "2018-11-15T10:36:50",
"canVote": false,
"author": {
"profileUrl": "https://disqus.com/by/mnaid-29e1c59be16c852670e3be302e8c303b/",
"isPrivate": false,
"isPrimary": true,
"about": "",
"username": "mnaid-29e1c59be16c852670e3be302e8c303b",
"url": "",
"id": "250739494",
"isAnonymous": false,
"avatar": {
"permalink": "https://disqus.com/api/users/avatars/mnaid-29e1c59be16c852670e3be302e8c303b.jpg",
"cache": "//a.disquscdn.com/1541535443/images/noavatar92.png",
"small": {
"permalink": "https://disqus.com/api/users/avatars/mnaid-29e1c59be16c852670e3be302e8c303b.jpg",
"cache": "//a.disquscdn.com/1541535443/images/noavatar32.png"
},
"large": {
"permalink": "https://disqus.com/api/users/avatars/mnaid-29e1c59be16c852670e3be302e8c303b.jpg",
"cache": "//a.disquscdn.com/1541535443/images/noavatar92.png"
},
"isCustom": false
},
"location": "",
"isPowerContributor": false,
"signedUrl": "",
"joinedAt": "2017-05-03T13:01:02",
"disable3rdPartyTrackers": false,
"name": "Mordecai"
},
"parent": null,
"isFlagged": false,
"likes": 0,
"id": "4196044068",
"isDeletedByAuthor": false,
"isDeleted": false,
"message": "<p>The crappest of crap deals.<br>Will never get through Parliament.</p><p>You are the weakest link May, goodbye.</p>",
"isEdited": false,
"isApproved": true
},
{
"thread": "7046384220",
"numReports": 0,
"editableUntil": "2018-11-22T10:28:13",
"sb": false,
"media": [],
"moderationLabels": [],
"isSpam": false,
"isHighlighted": false,
"dislikes": 0,
"points": 0,
"raw_message": "We are all just prisoners here of our own device. \ud83d\ude01",
"forum": "expressandstar",
"createdAt": "2018-11-15T10:28:13",
"canVote": false,
"author": {
"profileUrl": "https://disqus.com/by/mnaid-42ba513c42a0fd6558aa44b1de658140/",
"isPrivate": false,
"isPrimary": true,
"about": "",
"username": "mnaid-42ba513c42a0fd6558aa44b1de658140",
"url": "",
"id": "274657487",
"isAnonymous": false,
"avatar": {
"permalink": "https://disqus.com/api/users/avatars/mnaid-42ba513c42a0fd6558aa44b1de658140.jpg",
"cache": "//a.disquscdn.com/1541535443/images/noavatar92.png",
"small": {
"permalink": "https://disqus.com/api/users/avatars/mnaid-42ba513c42a0fd6558aa44b1de658140.jpg",
"cache": "//a.disquscdn.com/1541535443/images/noavatar32.png"
},
"large": {
"permalink": "https://disqus.com/api/users/avatars/mnaid-42ba513c42a0fd6558aa44b1de658140.jpg",
"cache": "//a.disquscdn.com/1541535443/images/noavatar92.png"
},
"isCustom": false
},
"location": "",
"isPowerContributor": false,
"signedUrl": "",
"joinedAt": "2017-12-20T16:45:10",
"disable3rdPartyTrackers": true,
"name": "chaffwolf"
},
"parent": 4195981074,
"isFlagged": false,
"likes": 0,
"id": "4196013534",
"isDeletedByAuthor": false,
"isDeleted": false,
"message": "<p>We are all just prisoners here of our own device. \ud83d\ude01</p>",
"isEdited": false,
"isApproved": true
},
{
"thread": "7046384220",
"numReports": 0,
"editableUntil": "2018-11-22T10:27:03",
"sb": false,
"media": [],
"moderationLabels": [],
"isSpam": false,
"isHighlighted": false,
"dislikes": 0,
"points": 0,
"raw_message": "Should be no deal end off.\nNobody could possibly be the winner so it's simple leave and that's it .\nTheir will always be wannabes who think they can do better in it for only their own powers not for the general country or the people of it .,",
"forum": "expressandstar",
"createdAt": "2018-11-15T10:27:03",
"canVote": false,
"author": {
"profileUrl": "https://disqus.com/by/mnaid-90ecce8d5dad4396f681182cb470872c/",
"isPrivate": false,
"isPrimary": true,
"about": "",
"username": "mnaid-90ecce8d5dad4396f681182cb470872c",
"url": "",
"id": "251694793",
"isAnonymous": false,
"avatar": {
"permalink": "https://disqus.com/api/users/avatars/mnaid-90ecce8d5dad4396f681182cb470872c.jpg",
"cache": "//a.disquscdn.com/1541535443/images/noavatar92.png",
"small": {
"permalink": "https://disqus.com/api/users/avatars/mnaid-90ecce8d5dad4396f681182cb470872c.jpg",
"cache": "//a.disquscdn.com/1541535443/images/noavatar32.png"
},
"large": {
"permalink": "https://disqus.com/api/users/avatars/mnaid-90ecce8d5dad4396f681182cb470872c.jpg",
"cache": "//a.disquscdn.com/1541535443/images/noavatar92.png"
},
"isCustom": false
},
"location": "",
"isPowerContributor": false,
"signedUrl": "",
"joinedAt": "2017-05-12T20:20:43",
"disable3rdPartyTrackers": true,
"name": "wanderer in eire"
},
"parent": null,
"isFlagged": false,
"likes": 0,
"id": "4196012501",
"isDeletedByAuthor": false,
"isDeleted": false,
"message": "<p>Should be no deal end off.<br>Nobody could possibly be the winner so it's simple leave and that's it .<br>Their will always be wannabes who think they can do better in it for only their own powers not for the general country or the people of it .,</p>",
"isEdited": false,
"isApproved": true
},
{
"thread": "7046384220",
"numReports": 0,
"editableUntil": "2018-11-22T10:26:42",
"sb": false,
"media": [],
"moderationLabels": [],
"isSpam": false,
"isHighlighted": false,
"dislikes": 0,
"points": 0,
"raw_message": "Damned if she does, damned if she doesn't. All for a cause she does not believe in.\nShe will go down in history whatever happens. \nThe question is: who better to lead the country at such an important juncture??\nAnswers on a postcard!",
"forum": "expressandstar",
"createdAt": "2018-11-15T10:26:42",
"canVote": false,
"author": {
"profileUrl": "https://disqus.com/by/mnaid-42ba513c42a0fd6558aa44b1de658140/",
"isPrivate": false,
"isPrimary": true,
"about": "",
"username": "mnaid-42ba513c42a0fd6558aa44b1de658140",
"url": "",
"id": "274657487",
"isAnonymous": false,
"avatar": {
"permalink": "https://disqus.com/api/users/avatars/mnaid-42ba513c42a0fd6558aa44b1de658140.jpg",
"cache": "//a.disquscdn.com/1541535443/images/noavatar92.png",
"small": {
"permalink": "https://disqus.com/api/users/avatars/mnaid-42ba513c42a0fd6558aa44b1de658140.jpg",
"cache": "//a.disquscdn.com/1541535443/images/noavatar32.png"
},
"large": {
"permalink": "https://disqus.com/api/users/avatars/mnaid-42ba513c42a0fd6558aa44b1de658140.jpg",
"cache": "//a.disquscdn.com/1541535443/images/noavatar92.png"
},
"isCustom": false
},
"location": "",
"isPowerContributor": false,
"signedUrl": "",
"joinedAt": "2017-12-20T16:45:10",
"disable3rdPartyTrackers": true,
"name": "chaffwolf"
},
"parent": null,
"isFlagged": false,
"likes": 0,
"id": "4196012237",
"isDeletedByAuthor": false,
"isDeleted": false,
"message": "<p>Damned if she does, damned if she doesn't. All for a cause she does not believe in.<br>She will go down in history whatever happens. <br>The question is: who better to lead the country at such an important juncture??<br>Answers on a postcard!</p>",
"isEdited": false,
"isApproved": true
},
{
"thread": "7046384220",
"numReports": 0,
"editableUntil": "2018-11-22T09:50:32",
"sb": false,
"media": [],
"moderationLabels": [],
"isSpam": false,
"isHighlighted": false,
"dislikes": 0,
"points": 0,
"raw_message": "Theresa May is using The Eagles Hotel California lyrics as the inspiration for her Brexit plan.\n\nYou can check out any time you like, but you can never leave.",
"forum": "expressandstar",
"createdAt": "2018-11-15T09:50:32",
"canVote": false,
"author": {
"profileUrl": "https://disqus.com/by/mnaid-e82a88d937e60267fd2c866b01131ada/",
"isPrivate": false,
"isPrimary": true,
"about": "",
"username": "mnaid-e82a88d937e60267fd2c866b01131ada",
"url": "",
"id": "250727584",
"isAnonymous": false,
"avatar": {
"permalink": "https://disqus.com/api/users/avatars/mnaid-e82a88d937e60267fd2c866b01131ada.jpg",
"cache": "//a.disquscdn.com/1541535443/images/noavatar92.png",
"small": {
"permalink": "https://disqus.com/api/users/avatars/mnaid-e82a88d937e60267fd2c866b01131ada.jpg",
"cache": "//a.disquscdn.com/1541535443/images/noavatar32.png"
},
"large": {
"permalink": "https://disqus.com/api/users/avatars/mnaid-e82a88d937e60267fd2c866b01131ada.jpg",
"cache": "//a.disquscdn.com/1541535443/images/noavatar92.png"
},
"isCustom": false
},
"location": "",
"isPowerContributor": false,
"signedUrl": "",
"joinedAt": "2017-05-03T10:27:23",
"disable3rdPartyTrackers": false,
"name": "Olly the cat"
},
"parent": null,
"isFlagged": false,
"likes": 0,
"id": "4195981074",
"isDeletedByAuthor": false,
"isDeleted": false,
"message": "<p>Theresa May is using The Eagles Hotel California lyrics as the inspiration for her Brexit plan.</p><p>You can check out any time you like, but you can never leave.</p>",
"isEdited": false,
"isApproved": true
}
],
"code": 0,
"cursor": {
"more": false,
"total": null,
"prev": null,
"hasNext": false,
"next": "1542275432008325:1:0",
"id": "1542275432008325:1:0",
"hasPrev": false
}
}
My main project is taking data from the JSON file and making a relational database with the data.
The top-level object in that JSON data is a dictionary. But you are trying to index it as though it were a list. (data[0]). Since 0 is not a key of the dictionary, trying to use it as a key results in a KeyError.
If you want the keys of the top-kevel object, use data.keys().
I am currently creating my data frame with the below commands
getting data from url
...
devices = get_device_data.json()
device_data = devices["data"]
p_dev = pd.DataFrame(device_data)
However the json dictionary has 60 columns in, of which I only want two of them, is there a way to specify which columns to include when creating a data frame, or any way I can achieve the desired result?
Thanks
EDIT: some sample data with some columns deleted. I actually only want the id and hostname columns
[
{
"id": 474378238
"account": "https: //www.****.com/api/v2/accounts/38021/",
"bsid": None,
"carrier": "BOB",
"carrier_id": "BOB BOB",
"channel": None,
"connection_state": "connected",
"gsn": "356853050758871",
"homecarrid": "BOB",
"hostname": "BOB1345345",
"is_asset": True,
"is_gps_supported": True,
"is_upgrade_available": False,
"is_upgrade_supported": True,
"ltebandwidth": "20 MHz",
"mac": None,
"serial": "356853050758871",
"service_type": "LTE",
"ssid": None,
"summary": "connected",
"txchannel": "19667",
"type": "mdm",
"uid": "f5a8da8f",
"updated_at": "2018-08-17T11:19:57.019938+00:00",
"uptime": 86412.8558200002,
},
{
"id": 5674657356
"account": "https: //www.****.com/api/v2/accounts/38021/",
"bsid": None,
"carrier": "BOB",
"carrier_id": "BOB BOB",
"channel": None,
"connection_state": "connected",
"gsn": "356853050758871",
"homecarrid": "BOB",
"hostname": "BOB10765",
"is_asset": True,
"is_gps_supported": True,
"is_upgrade_available": False,
"is_upgrade_supported": True,
"ltebandwidth": "20 MHz",
"mac": None,
"serial": "356853050758871",
"service_type": "LTE",
"ssid": None,
"summary": "connected",
"txchannel": "19667",
"type": "mdm",
"uid": "f5a8da8f",
"updated_at": "2018-08-17T11:19:57.019938+00:00",
"uptime": 86412.8558200002,
},
{
"id": 5674657465
"account": "https: //www.****.com/api/v2/accounts/38021/",
"bsid": None,
"carrier": "BOB",
"carrier_id": "BOB BOB",
"channel": None,
"connection_state": "connected",
"gsn": "356853050758871",
"homecarrid": "BOB",
"hostname": "BOB10453453",
"is_asset": True,
"is_gps_supported": True,
"is_upgrade_available": False,
"is_upgrade_supported": True,
"ltebandwidth": "20 MHz",
"mac": None,
"serial": "356853050758871",
"service_type": "LTE",
"ssid": None,
"summary": "connected",
"txchannel": "19667",
"type": "mdm",
"uid": "f5a8da8f",
"updated_at": "2018-08-17T11:19:57.019938+00:00",
"uptime": 86412.8558200002,
},
{
"id": 9756756756
"account": "https: //www.****.com/api/v2/accounts/38021/",
"bsid": None,
"carrier": "BOB",
"carrier_id": "BOB BOB",
"channel": None,
"connection_state": "connected",
"gsn": "356853050758871",
"homecarrid": "BOB",
"hostname": "BOB100133",
"is_asset": True,
"is_gps_supported": True,
"is_upgrade_available": False,
"is_upgrade_supported": True,
"ltebandwidth": "20 MHz",
"mac": None,
"serial": "356853050758871",
"service_type": "LTE",
"ssid": None,
"summary": "connected",
"txchannel": "19667",
"type": "mdm",
"uid": "f5a8da8f",
"updated_at": "2018-08-17T11:19:57.019938+00:00",
"uptime": 86412.8558200002,
},
]
Use list comprehension with dict comprehension for filtering by colums names:
L is list of input data
device_data = [{k: v for k, v in x.items() if k in ['type','id']} for x in L]
print (device_data)
[{'id': 474378238, 'type': 'mdm'}, {'id': 5674657356, 'type': 'mdm'},
{'id': 5674657465, 'type': 'mdm'}, {'id': 9756756756, 'type': 'mdm'}]
df = pd.DataFrame(device_data)
print (df)
id type
0 474378238 mdm
1 5674657356 mdm
2 5674657465 mdm
3 9756756756 mdm
You can do it by deleting those columns from the data frame by the column header.
Example: p_dev.drop(['Column_1','Column_2','column_2'], axis = 1, inplace = True)
Another Way to do it to write only those column headers in a list which are needed and then overwrite the existing dataframe
Example:
col_list = ['Column_1', 'Column_3']
p_def=p_def[col_list]
I'm new to python and I've been trying to parse the output of from an F5 API request and I'm struggling a bit. I would like to extract all the "name" and "destination" values and save those to a file. This should be a name and the corresponding IP address. From the output below, it would be:
test-account 10.1.1.1
test-account_443 10.1.1.5
I can extract the individual elements, but I'm struggling to figure out how to iterate over the output file so I can get all of the key values I want. I've cut the output short to make it easier to read.
parsed_json = json.loads(data)
destination = parsed_json['items'][0]['destination']
name = parsed_json['items'][0]['name']
This only extracts one key value from the whole output file. Many thanks in advance.
{
"kind": "tm:ltm:virtual:virtualcollectionstate",
"items": [
{
"kind": "tm:ltm:virtual:virtualstate",
"name": "test-account",
"partition": "Common",
"fullPath": "/Common/test-account",
"generation": 1,
"addressStatus": "yes",
"autoLasthop": "default",
"cmpEnabled": "yes",
"connectionLimit": 0,
"destination": "/Common/10.1.1.1:80",
"enabled": true,
"gtmScore": 0,
"ipProtocol": "tcp",
"mask": "255.255.255.255",
"mirror": "disabled",
"mobileAppTunnel": "disabled",
"nat64": "disabled",
"pool": "/Common/test-account",
"rateLimit": "disabled",
"rateLimitDstMask": 0,
"rateLimitMode": "object",
"rateLimitSrcMask": 0,
"serviceDownImmediateAction": "none",
"source": "0.0.0.0/0",
"sourceAddressTranslation": {
"type": "automap"
},
"sourcePort": "preserve",
"synCookieStatus": "not-activated",
"translateAddress": "enabled",
"translatePort": "enabled",
"vlansDisabled": true,
"vsIndex": 2,
"rules": [
"/Common/redirect-http-to-https"
],
"rulesReference": [
{
"link":
}
],
"policiesReference": {
"link":
"isSubcollection": true
},
"profilesReference": {
"link":
"isSubcollection": true
}
},
{
"kind": "tm:ltm:virtual:virtualstate",
"name": "test-account_443",
"partition": "Common",
"fullPath": "/Common/test-account_443",
"generation": 1,
"selfLink":
"addressStatus": "yes",
"autoLasthop": "default",
"cmpEnabled": "yes",
"connectionLimit": 0,
"destination": "/Common/10.1.1.5:443",
"enabled": true,
"gtmScore": 0,
"ipProtocol": "tcp",
"mask": "255.255.255.255",
"mirror": "disabled",
"mobileAppTunnel": "disabled",
"nat64": "disabled",
"pool": "/Common/test-account",
"poolReference": {
"link":
},
"rateLimit": "disabled",
"rateLimitDstMask": 0,
"rateLimitMode": "object",
"rateLimitSrcMask": 0,
"serviceDownImmediateAction": "none",
"source": "0.0.0.0/0",
"sourceAddressTranslation": {
"type": "automap"
},
"sourcePort": "preserve",
"synCookieStatus": "not-activated",
"translateAddress": "enabled",
"translatePort": "enabled",
"vlansDisabled": true,
"vsIndex": 3,
"policiesReference": {
"link":
"isSubcollection": true
},
"profilesReference": {
"link":
"isSubcollection": true
}
}
This should help. You can iterate over the "items" key and get the required information
Ex:
parsed_json = {
"kind": "tm:ltm:virtual:virtualcollectionstate",
"items": [
{
"kind": "tm:ltm:virtual:virtualstate",
"name": "test-account",
"partition": "Common",
"fullPath": "/Common/test-account",
"generation": 1,
"addressStatus": "yes",
"autoLasthop": "default",
"cmpEnabled": "yes",
"connectionLimit": 0,
"destination": "/Common/10.1.1.1:80",
"enabled": True,
"gtmScore": 0,
"ipProtocol": "tcp",
"mask": "255.255.255.255",
"mirror": "disabled",
"mobileAppTunnel": "disabled",
"nat64": "disabled",
"pool": "/Common/test-account",
"rateLimit": "disabled",
"rateLimitDstMask": 0,
"rateLimitMode": "object",
"rateLimitSrcMask": 0,
"serviceDownImmediateAction": "none",
"source": "0.0.0.0/0",
"sourceAddressTranslation": {
"type": "automap"
},
"sourcePort": "preserve",
"synCookieStatus": "not-activated",
"translateAddress": "enabled",
"translatePort": "enabled",
"vlansDisabled": True,
"vsIndex": 2,
"rules": [
"/Common/redirect-http-to-https"
],
"rulesReference": [
{
"link": True
}
],
"policiesReference": {
"link": True,
"isSubcollection": True
},
"profilesReference": {
"link": True,
"isSubcollection": True
}
},
{
"kind": "tm:ltm:virtual:virtualstate",
"name": "test-account_443",
"partition": "Common",
"fullPath": "/Common/test-account_443",
"generation": 1,
"selfLink": True,
"addressStatus": "yes",
"autoLasthop": "default",
"cmpEnabled": "yes",
"connectionLimit": 0,
"destination": "/Common/10.1.1.5:443",
"enabled": True,
"gtmScore": 0,
"ipProtocol": "tcp",
"mask": "255.255.255.255",
"mirror": "disabled",
"mobileAppTunnel": "disabled",
"nat64": "disabled",
"pool": "/Common/test-account",
"poolReference": {
"link": True
},
"rateLimit": "disabled",
"rateLimitDstMask": 0,
"rateLimitMode": "object",
"rateLimitSrcMask": 0,
"serviceDownImmediateAction": "none",
"source": "0.0.0.0/0",
"sourceAddressTranslation": {
"type": "automap"
},
"sourcePort": "preserve",
"synCookieStatus": "not-activated",
"translateAddress": "enabled",
"translatePort": "enabled",
"vlansDisabled": True,
"vsIndex": 3,
"policiesReference": {
"link": True,
"isSubcollection": True
},
"profilesReference": {
"link": True,
"isSubcollection": True
}
}]
}
res = []
for value in parsed_json["items"]:
print(value["name"])
print(value["destination"])
print(value["destination"].split("/")[-1].split(":")[0]) #if you need only the IP.
Output:
test-account
/Common/10.1.1.1:80
10.1.1.1
test-account_443
/Common/10.1.1.5:443
10.1.1.5
I'm having trouble iterating through a JSON inside the django template.
My JOSN looks like,
{
"main_cats": [
{
"pk": 1,
"model": "BeeForms.maincategory",
"fields": {
"is_visible": true,
"name": "Test Main",
"order_by_asc": true,
"base_category": 1,
"help_text": "",
"is_seller_field": false,
"position": 10,
"show_seller": true,
"order_by_type": true
}
}
],
"sub_cats": [
{
"pk": 15,
"model": "BeeForms.subcategory",
"fields": {
"regex": "[0-9a-zA-Z]",
"library_id": "TextBox",
"input_length": 255,
"name": "TextBox",
"order_by_asc": false,
"field_type": "text",
"main_category": 1,
"is_active": true,
"is_visible": true,
"help_text": "",
"position": 10,
"is_required": true,
"field_display_type": "input",
"order_by_type": true,
"has_enumerations": false
}
},
{
"pk": 16,
"model": "BeeForms.subcategory",
"fields": {
"regex": "[A-Za-z0-9]",
"library_id": "SelectBox",
"input_length": 255,
"name": "SelectBox",
"order_by_asc": false,
"field_type": "",
"main_category": 1,
"is_active": true,
"is_visible": true,
"help_text": "",
"position": 10,
"is_required": false,
"field_display_type": "select",
"order_by_type": true,
"has_enumerations": true
}
},
{
"pk": 18,
"model": "BeeForms.subcategory",
"fields": {
"regex": "[0-9a-zA-Z]",
"library_id": "TextArea",
"input_length": 255,
"name": "TextArea",
"order_by_asc": false,
"field_type": "varchar",
"main_category": 1,
"is_active": true,
"is_visible": true,
"help_text": "",
"position": 10,
"is_required": false,
"field_display_type": "textarea",
"order_by_type": true,
"has_enumerations": false
}
},
{
"pk": 22,
"model": "BeeForms.subcategory",
"fields": {
"regex": "[A-Za-z0-9]",
"library_id": "Checkbox",
"input_length": 255,
"name": "Checkbox",
"order_by_asc": false,
"field_type": "",
"main_category": 1,
"is_active": true,
"is_visible": true,
"help_text": "",
"position": 10,
"is_required": false,
"field_display_type": "checkbox",
"order_by_type": true,
"has_enumerations": true
}
}
],
"base_cats": [
{
"pk": 1,
"model": "BeeForms.basecategory",
"fields": {
"form_type": 1,
"position": 10,
"order_by_asc": true,
"name": "Sales Information",
"order_by_type": true
}
},
{
"pk": 2,
"model": "BeeForms.basecategory",
"fields": {
"form_type": 1,
"position": 20,
"order_by_asc": true,
"name": "Listing information",
"order_by_type": true
}
},
{
"pk": 3,
"model": "BeeForms.basecategory",
"fields": {
"form_type": 1,
"position": 30,
"order_by_asc": true,
"name": "Land & Tax",
"order_by_type": true
}
},
{
"pk": 4,
"model": "BeeForms.basecategory",
"fields": {
"form_type": 1,
"position": 40,
"order_by_asc": true,
"name": "Exterior Construction",
"order_by_type": true
}
},
{
"pk": 5,
"model": "BeeForms.basecategory",
"fields": {
"form_type": 1,
"position": 50,
"order_by_asc": true,
"name": "Interior/Utilities",
"order_by_type": true
}
},
{
"pk": 6,
"model": "BeeForms.basecategory",
"fields": {
"form_type": 1,
"position": 60,
"order_by_asc": true,
"name": "HOA/Community",
"order_by_type": true
}
},
{
"pk": 7,
"model": "BeeForms.basecategory",
"fields": {
"form_type": 1,
"position": 70,
"order_by_asc": true,
"name": "Remarks",
"order_by_type": true
}
}
],
"enum_content": [
{
"pk": 58,
"model": "BeeForms.enumerationcontent",
"fields": {
"library_id": "SelectYesA1",
"name": "SelectYesA1",
"order_by_asc": false,
"is_visible": true,
"is_active": true,
"enum_type_category": 53,
"position": 10,
"order_by_type": true
}
},
{
"pk": 59,
"model": "BeeForms.enumerationcontent",
"fields": {
"library_id": "SelectNoA1",
"name": "SelectNoA1",
"order_by_asc": false,
"is_visible": true,
"is_active": true,
"enum_type_category": 53,
"position": 10,
"order_by_type": true
}
},
{
"pk": 60,
"model": "BeeForms.enumerationcontent",
"fields": {
"library_id": "SelectCheckBoxYes",
"name": "SelectCheckBoxYes",
"order_by_asc": false,
"is_visible": true,
"is_active": true,
"enum_type_category": 54,
"position": 10,
"order_by_type": true
}
},
{
"pk": 61,
"model": "BeeForms.enumerationcontent",
"fields": {
"library_id": "SelectCheckBoxNo",
"name": "SelectCheckBoxNo",
"order_by_asc": false,
"is_visible": true,
"is_active": true,
"enum_type_category": 54,
"position": 10,
"order_by_type": true
}
}
],
"enums": [
{
"pk": 20,
"model": "BeeForms.enumerations",
"fields": {
"library_id": "SelectYes",
"name": "SelectYes",
"order_by_asc": false,
"is_visible": true,
"has_type": true,
"is_active": true,
"position": 10,
"order_by_type": true,
"sub_category": 16
}
},
{
"pk": 21,
"model": "BeeForms.enumerations",
"fields": {
"library_id": "SelectNo",
"name": "SelectNo",
"order_by_asc": false,
"is_visible": true,
"has_type": true,
"is_active": true,
"position": 10,
"order_by_type": true,
"sub_category": 16
}
},
{
"pk": 30,
"model": "BeeForms.enumerations",
"fields": {
"library_id": "Yes",
"name": "Yes",
"order_by_asc": false,
"is_visible": true,
"has_type": true,
"is_active": true,
"position": 10,
"order_by_type": true,
"sub_category": 22
}
},
{
"pk": 31,
"model": "BeeForms.enumerations",
"fields": {
"library_id": "No",
"name": "No",
"order_by_asc": false,
"is_visible": true,
"has_type": false,
"is_active": true,
"position": 10,
"order_by_type": true,
"sub_category": 22
}
}
],
"enum_types": [
{
"pk": 53,
"model": "BeeForms.enumerationtype",
"fields": {
"regex": "[A-Za-z0-9]",
"is_visible": true,
"name": "SelectYesA",
"library_id": "SelectYesA",
"field_type": "",
"is_active": true,
"has_content": true,
"enum_category": 20,
"field_display_type": "select",
"input_length": 255
}
},
{
"pk": 54,
"model": "BeeForms.enumerationtype",
"fields": {
"regex": "[A-Za-z0-9]",
"is_visible": true,
"name": "",
"library_id": "",
"field_type": "",
"is_active": true,
"has_content": true,
"enum_category": 21,
"field_display_type": "checkbox",
"input_length": 255
}
}
]
}
The code I use to create the json file is as follows.
def serialize(self, instance_name, form, destination):
""" Seralizes the information passed in."""
# we need to make sure that the directory is real.
if not os.path.exists(destination):
os.makedirs(destination)
# Lets grab all the information needed and serailize it all at the same time.
base_cats = serializers.serialize('json', BaseCategory.objects.all().filter(form_type_id=form.id).order_by('id'))
main_cats = serializers.serialize('json', MainCategory.objects.all().order_by('id'))
sub_cats = serializers.serialize('json', SubCategory.objects.all().order_by('id'))
enums = serializers.serialize('json', Enumerations.objects.all().order_by('id'))
enum_types = serializers.serialize('json', EnumerationType.objects.all().order_by('id'))
enum_content = serializers.serialize('json', EnumerationContent.objects.all().order_by('id'))
# now that they are all serailized make it into a list of simplejson
base_list = simplejson.loads(base_cats)
main_list = simplejson.loads(main_cats)
sub_list = simplejson.loads(sub_cats)
enums_list = simplejson.loads(enums)
enum_type_list = simplejson.loads(enum_types)
enum_content_list = simplejson.loads(enum_content)
# now we combine them all together...
self.json_data = simplejson.dumps({'base_cats': base_list, 'main_cats': main_list, 'sub_cats': sub_list,
'enums': enums_list, 'enum_types': enum_type_list,
'enum_content': enum_content_list}, indent=4)
print(self.json_data)
# now that we combined them into json_data. Lets write it out to a file.
with open(destination + instance_name + '.json', 'w') as json_write:
json_write.write(self.json_data)
I load the JSON into a Python Object with the following.
with open(instance_location + '.json', 'r') as json_read:
self.json_data = json_read.read()
# now lets take the data and load into json array
data = simplejson.loads(self.json_data)
# now lets grab each list.
base_cats = data['base_cats']
main_cats = data['main_cats']
sub_cats = data['sub_cats']
enums = data['enums']
enum_types = data['enum_types']
enum_content = data['enum_content']
# lets return it all!
return base_cats, main_cats, sub_cats, enums, enum_types, enum_content
In straight Python I'm able to loop through (for example base_cats) and get the information I need with the following for loop
for base in base_dict:
print base['pk']
print base['fields']['name']
When I try that for loop in the django template I get the error of:
Could not parse the remainder: '['pk']' from 'base['pk']'
With how the JSON is structered how would I loop through it in the django template as the following is not working.
{% for base in base_cats %}
{{ base['pk'] }}
{% endfor %}
Access the fields with the dot operator in the template, not with brackets:
{% for base in base_cats %}
{{ base.pk }}
{% endfor %}
For reference:
https://docs.djangoproject.com/en/1.10/ref/templates/language/#template-variables