Im using Backand to store my data. I have an object, Events, that references another object, Locations.
{
"name": "events",
"fields": {
"eventCommentsId": {
"collection": "comments",
"via": "eventId"
},
"tags": {
"collection": "events_tags",
"via": "event"
},
"users": {
"collection": "users_events",
"via": "event"
},
"name": {
"type": "string"
},
"date": {
"type": "datetime"
},
"time": {
"type": "datetime"
},
"info": {
"type": "text"
},
"locationId": {
"object": "locations"
}
},
{
"name": "locations",
"fields": {
"events": {
"collection": "events",
"via": "locationId"
},
"name": {
"type": "text"
},
"geo": {
"type": "point"
}
}
}
When I try to display the location of the event, I can only get the value of locationID. I want the actual name of the location, not the id. How do I do that?
<ion-list>
<ion-item class="item item-thumbnail-left" ng-repeat="event in events" type="item-text-wrap" href="#/event-detail/{{event.id}}">
<h2>{{event.name}}</h2>
<p><i class="ion-location"></i> {{event.locationId.name}}</p>
<ion-option-button class="button-assertive" ng-click="deleteEvent(event.id)">
Delete
</ion-option-button>
</ion-item>
</ion-list>
angular code
.service('EventService', function ($http, Backand) {
var baseUrl = '/1/objects/';
var objectName = 'events/';
function getUrl() {
return Backand.getApiUrl() + baseUrl + objectName;
}
function getUrlForId(id) {
return getUrl() + id;
}
getEvents = function () {
return $http.get(getUrl());
};
addEvent = function(event) {
return $http.post(getUrl(), event);
}
deleteEvent = function (id) {
return $http.delete(getUrlForId(id));
};
getEvent = function (id) {
return $http.get(getUrlForId(id));
};
return {
getEvents: getEvents,
addEvent: addEvent,
deleteEvent: deleteEvent,
getEvent: getEvent
}
})
.controller('FeedCtrl', ['$scope', '$ionicModal', '$ionicSideMenuDelegate', 'EventService', function($scope, $ionicModal, $ionicSideMenuDelegate, EventService) {
$scope.events = [];
$scope.input = {};
function getAllEvents() {
EventService.getEvents()
.then(function (result) {
$scope.events = result.data.data;
});
}
$scope.addEvent = function() {
EventService.addEvent($scope.input)
.then(function(result) {
$scope.input = {};
getAllEvents();
});
}
$scope.deleteEvent = function(id) {
EventService.deleteEvent(id)
.then(function (result) {
getAllEvents();
});
}
getAllEvents();
}])
There are two options. You can either use the descriptive value in the __metadata of each object like this:
request: https://api.backand.com/1/objects/events?pageSize=20&pageNumber=1
response:
{
"totalRows": 2,
"data": [
{
"__metadata": {
"id": "1",
"fields": {
"id": {
"type": "int",
"unique": true
},
"name": {
"type": "string"
},
"date": {
"type": "datetime"
},
"time": {
"type": "datetime"
},
"info": {
"type": "text"
},
"locationId": {
"object": "locations"
}
},
"descriptives": {
"locationId": {
"label": "Madison Square Garden",
"value": "1"
}
},
"dates": {
"date": "",
"time": ""
}
},
"id": 1,
"name": "knicks vs warriors",
"date": null,
"time": null,
"info": "",
"locationId": "1"
},
{
"__metadata": {
"id": "2",
"fields": {
"id": {
"type": "int",
"unique": true
},
"name": {
"type": "string"
},
"date": {
"type": "datetime"
},
"time": {
"type": "datetime"
},
"info": {
"type": "text"
},
"locationId": {
"object": "locations"
}
},
"descriptives": {
"locationId": {
"label": "Madison Square Garden",
"value": "1"
}
},
"dates": {
"date": "",
"time": ""
}
},
"id": 2,
"name": "knicks vs cavs",
"date": null,
"time": null,
"info": "",
"locationId": "1"
}
]
}
or you can do a deep request and get the value in the relatedObjects
request: https://api.backand.com/1/objects/events?pageSize=20&pageNumber=1&deep=true
response:
{
"totalRows": 2,
"data": [
{
"__metadata": {
"id": "1",
"fields": {
"id": {
"type": "int",
"unique": true
},
"name": {
"type": "string"
},
"date": {
"type": "datetime"
},
"time": {
"type": "datetime"
},
"info": {
"type": "text"
},
"locationId": {
"object": "locations"
}
},
"descriptives": {
"locationId": {
"label": "Madison Square Garden",
"value": "1"
}
},
"dates": {
"date": "",
"time": ""
}
},
"id": 1,
"name": "knicks vs warriors",
"date": null,
"time": null,
"info": "",
"locationId": "1"
},
{
"__metadata": {
"id": "2",
"fields": {
"id": {
"type": "int",
"unique": true
},
"name": {
"type": "string"
},
"date": {
"type": "datetime"
},
"time": {
"type": "datetime"
},
"info": {
"type": "text"
},
"locationId": {
"object": "locations"
}
},
"descriptives": {
"locationId": {
"label": "Madison Square Garden",
"value": "1"
}
},
"dates": {
"date": "",
"time": ""
}
},
"id": 2,
"name": "knicks vs cavs",
"date": null,
"time": null,
"info": "",
"locationId": "1"
}
],
"relatedObjects": {
"locations": {
"1": {
"__metadata": {
"id": "1",
"fields": {
"id": {
"type": "int",
"unique": true
},
"events": {
"collection": "events",
"via": "locationId"
},
"name": {
"type": "text"
},
"geo": {
"type": "point"
}
},
"descriptives": {},
"dates": {}
},
"id": 1,
"events": null,
"name": "Madison Square Garden",
"geo": [
40.7505,
73.9934
]
}
}
}
}
search for Madison Square Garden as the name of the location to understand the JSON structure.
You can set the descriptive field in the Object Settings
I am working with a reports API from an Application which converts an HTML table into JSON using a method very similar to that shown in posts in Stack Overflow (example: HTML Table to JSON).
The JSON has an array of columns (for the NAMES of VALUES), then there is an array of rows which contain cells (for the VALUES).
I want to map this report to a canonical data model but it is horrible to work with. What I want to do is run some sort of script on the JSON which reverse what the original script put in place and turns it into an array that contains individual records, much like the rows of a CSV file.
Here's an example of a report I am referring to - horrible isn't it :)
My Question
Is there a way of turning this format of JSON (where it has an array for column names, an array for sections and inside an array of rows which relate to the column names), into a table of some sort?
{
"Header": {
"Time": "2016-03-30T16:10:19-07:00",
"ReportName": "GeneralLedger",
"ReportBasis": "Accrual",
"StartPeriod": "2016-01-01",
"EndPeriod": "2016-03-31",
"Currency": "GBP",
"Option": [
{
"Name": "NoReportData",
"Value": "false"
}
]
},
"Columns": {
"Column": [
{
"ColTitle": "Date",
"ColType": "tx_date"
},
{
"ColTitle": "Transaction Type",
"ColType": "txn_type"
},
{
"ColTitle": "No.",
"ColType": "doc_num"
},
{
"ColTitle": "Name",
"ColType": "name"
},
{
"ColTitle": "Memo/Description",
"ColType": "memo"
},
{
"ColTitle": "Split",
"ColType": "split_acc"
},
{
"ColTitle": "Amount",
"ColType": "subt_nat_amount"
},
{
"ColTitle": "Balance",
"ColType": "rbal_nat_amount"
}
]
},
"Rows": {
"Row": [
{
"Header": {
"ColData": [
{
"value": "Current",
"id": "144"
},
{
"value": ""
},
{
"value": ""
},
{
"value": ""
},
{
"value": ""
},
{
"value": ""
},
{
"value": ""
},
{
"value": ""
}
]
},
"Rows": {
"Row": [
{
"ColData": [
{
"value": "2016-03-16"
},
{
"value": "Bill Payment (Cheque)",
"id": "181"
},
{
"value": "1"
},
{
"value": "Teddy's T Shirt Supplier",
"id": "70"
},
{
"value": "104478"
},
{
"value": "Creditors",
"id": "138"
},
{
"value": "-600.0"
},
{
"value": "-600.0"
}
],
"type": "Data"
},
{
"ColData": [
{
"value": "2016-03-17"
},
{
"value": "Bill Payment (Cheque)",
"id": "184"
},
{
"value": "2"
},
{
"value": "Teddy's T Shirt Supplier",
"id": "70"
},
{
"value": "104478"
},
{
"value": "Creditors",
"id": "138"
},
{
"value": "-120.0"
},
{
"value": "-720.0"
}
],
"type": "Data"
},
{
"ColData": [
{
"value": "2016-03-17"
},
{
"value": "Deposit",
"id": "180"
},
{
"value": ""
},
{
"value": "",
"id": ""
},
{
"value": "Opening Balance"
},
{
"value": "Opening Balance Equity",
"id": "137"
},
{
"value": "2400.0"
},
{
"value": "1680.0"
}
],
"type": "Data"
},
{
"ColData": [
{
"value": "2016-03-23"
},
{
"value": "Payment",
"id": "186"
},
{
"value": "345678"
},
{
"value": "Maxamillion Enterprises",
"id": "68"
},
{
"value": ""
},
{
"value": "Debtors",
"id": "140"
},
{
"value": "216.0"
},
{
"value": "1896.0"
}
],
"type": "Data"
}
]
},
"Summary": {
"ColData": [
{
"value": "Total for Current"
},
{
"value": ""
},
{
"value": ""
},
{
"value": ""
},
{
"value": ""
},
{
"value": ""
},
{
"value": "1896.0"
},
{
"value": ""
}
]
},
"type": "Section"
},
{
"Header": {
"ColData": [
{
"value": "Debtors",
"id": "140"
},
{
"value": ""
},
{
"value": ""
},
{
"value": ""
},
{
"value": ""
},
{
"value": ""
},
{
"value": ""
},
{
"value": ""
}
]
},
"Rows": {
"Row": [
{
"ColData": [
{
"value": "2016-03-16"
},
{
"value": "Invoice",
"id": "176"
},
{
"value": "1014"
},
{
"value": "Maxamillion Enterprises",
"id": "68"
},
{
"value": ""
},
{
"value": "-Split-",
"id": ""
},
{
"value": "216.0"
},
{
"value": "216.0"
}
],
"type": "Data"
},
{
"ColData": [
{
"value": "2016-03-16"
},
{
"value": "Invoice",
"id": "179"
},
{
"value": "1015"
},
{
"value": "Hope Reality Limited",
"id": "69"
},
{
"value": ""
},
{
"value": "-Split-",
"id": ""
},
{
"value": "108.0"
},
{
"value": "324.0"
}
],
"type": "Data"
},
{
"ColData": [
{
"value": "2016-03-23"
},
{
"value": "Payment",
"id": "186"
},
{
"value": "345678"
},
{
"value": "Maxamillion Enterprises",
"id": "68"
},
{
"value": ""
},
{
"value": "Current",
"id": "144"
},
{
"value": "-216.0"
},
{
"value": "108.0"
}
],
"type": "Data"
}
]
},
"Summary": {
"ColData": [
{
"value": "Total for Debtors"
},
{
"value": ""
},
{
"value": ""
},
{
"value": ""
},
{
"value": ""
},
{
"value": ""
},
{
"value": "108.0"
},
{
"value": ""
}
]
},
"type": "Section"
},
{
"Header": {
"ColData": [
{
"value": "Stock Asset",
"id": "136"
},
{
"value": ""
},
{
"value": ""
},
{
"value": ""
},
{
"value": ""
},
{
"value": ""
},
{
"value": ""
},
{
"value": ""
}
]
},
"Rows": {
"Row": [
{
"ColData": [
{
"value": "2016-03-01"
},
{
"value": "Stock Starting Value",
"id": "173"
},
{
"value": "START"
},
{
"value": "",
"id": ""
},
{
"value": "Round Neck T Shirt - Opening stock and value"
},
{
"value": "Opening Balance Equity",
"id": "137"
},
{
"value": "0.0"
},
{
"value": "0.0"
}
],
"type": "Data"
},
{
"ColData": [
{
"value": "2016-03-16"
},
{
"value": "Invoice",
"id": "179"
},
{
"value": "1015"
},
{
"value": "Hope Reality Limited",
"id": "69"
},
{
"value": "Round Neck T Shirt"
},
{
"value": "Debtors",
"id": "140"
},
{
"value": "-12.0"
},
{
"value": "-12.0"
}
],
"type": "Data"
},
{
"ColData": [
{
"value": "2016-03-16"
},
{
"value": "Invoice",
"id": "176"
},
{
"value": "1014"
},
{
"value": "Maxamillion Enterprises",
"id": "68"
},
{
"value": "Round Neck T Shirt"
},
{
"value": "Debtors",
"id": "140"
},
{
"value": "-24.0"
},
{
"value": "-36.0"
}
],
"type": "Data"
},
{
"ColData": [
{
"value": "2016-03-17"
},
{
"value": "Stock Qty Adjust",
"id": "177"
},
{
"value": "2"
},
{
"value": "",
"id": ""
},
{
"value": ""
},
{
"value": "Stock Shrinkage",
"id": "141"
},
{
"value": "0.0"
},
{
"value": "-36.0"
}
],
"type": "Data"
},
{
"ColData": [
{
"value": "2016-03-17"
},
{
"value": "Stock Qty Adjust",
"id": "182"
},
{
"value": "3"
},
{
"value": "",
"id": ""
},
{
"value": ""
},
{
"value": "Stock Shrinkage",
"id": "141"
},
{
"value": "-36.0"
},
{
"value": "-72.0"
}
],
"type": "Data"
},
{
"ColData": [
{
"value": "2016-03-17"
},
{
"value": "Stock Qty Adjust",
"id": "182"
},
{
"value": "3"
},
{
"value": "",
"id": ""
},
{
"value": ""
},
{
"value": "Stock Shrinkage",
"id": "141"
},
{
"value": "-564.0"
},
{
"value": "-636.0"
}
],
"type": "Data"
},
{
"ColData": [
{
"value": "2016-03-17"
},
{
"value": "Stock Qty Adjust",
"id": "177"
},
{
"value": "2"
},
{
"value": "",
"id": ""
},
{
"value": ""
},
{
"value": "Stock Shrinkage",
"id": "141"
},
{
"value": "600.0"
},
{
"value": "-36.0"
}
],
"type": "Data"
},
{
"ColData": [
{
"value": "2016-03-17"
},
{
"value": "Stock Qty Adjust",
"id": "182"
},
{
"value": "3"
},
{
"value": "",
"id": ""
},
{
"value": ""
},
{
"value": "Stock Shrinkage",
"id": "141"
},
{
"value": "0.0"
},
{
"value": "-36.0"
}
],
"type": "Data"
},
{
"ColData": [
{
"value": "2016-03-17"
},
{
"value": "Bill",
"id": "178"
},
{
"value": ""
},
{
"value": "Teddy's T Shirt Supplier",
"id": "70"
},
{
"value": "Round Neck T Shirt"
},
{
"value": "Creditors",
"id": "138"
},
{
"value": "600.0"
},
{
"value": "564.0"
}
],
"type": "Data"
}
]
},
"Summary": {
"ColData": [
{
"value": "Total for Stock Asset"
},
{
"value": ""
},
{
"value": ""
},
{
"value": ""
},
{
"value": ""
},
{
"value": ""
},
{
"value": "564.0"
},
{
"value": ""
}
]
},
"type": "Section"
},
{
"Header": {
"ColData": [
{
"value": "Creditors",
"id": "138"
},
{
"value": ""
},
{
"value": ""
},
{
"value": ""
},
{
"value": ""
},
{
"value": ""
},
{
"value": ""
},
{
"value": ""
}
]
},
"Rows": {
"Row": [
{
"ColData": [
{
"value": "2016-03-16"
},
{
"value": "Bill Payment (Cheque)",
"id": "181"
},
{
"value": "1"
},
{
"value": "Teddy's T Shirt Supplier",
"id": "70"
},
{
"value": ""
},
{
"value": "Current",
"id": "144"
},
{
"value": "-600.0"
},
{
"value": "-600.0"
}
],
"type": "Data"
},
{
"ColData": [
{
"value": "2016-03-17"
},
{
"value": "Bill Payment (Cheque)",
"id": "184"
},
{
"value": "2"
},
{
"value": "Teddy's T Shirt Supplier",
"id": "70"
},
{
"value": ""
},
{
"value": "Current",
"id": "144"
},
{
"value": "-120.0"
},
{
"value": "-720.0"
}
],
"type": "Data"
},
{
"ColData": [
{
"value": "2016-03-17"
},
{
"value": "Bill",
"id": "185"
},
{
"value": ""
},
{
"value": "British Power",
"id": "72"
},
{
"value": ""
},
{
"value": "Utilities",
"id": "129"
},
{
"value": "192.15"
},
{
"value": "-527.85"
}
],
"type": "Data"
},
{
"ColData": [
{
"value": "2016-03-17"
},
{
"value": "Bill",
"id": "183"
},
{
"value": ""
},
{
"value": "Printing Ink Supplies",
"id": "71"
},
{
"value": ""
},
{
"value": "-Split-",
"id": ""
},
{
"value": "1920.0"
},
{
"value": "1392.15"
}
],
"type": "Data"
},
{
"ColData": [
{
"value": "2016-03-17"
},
{
"value": "Bill",
"id": "178"
},
{
"value": ""
},
{
"value": "Teddy's T Shirt Supplier",
"id": "70"
},
{
"value": ""
},
{
"value": "Stock Asset",
"id": "136"
},
{
"value": "720.0"
},
{
"value": "2112.15"
}
],
"type": "Data"
}
]
},
"Summary": {
"ColData": [
{
"value": "Total for Creditors"
},
{
"value": ""
},
{
"value": ""
},
{
"value": ""
},
{
"value": ""
},
{
"value": ""
},
{
"value": "2112.15"
},
{
"value": ""
}
]
},
"type": "Section"
},
{
"Header": {
"ColData": [
{
"value": "VAT Control",
"id": "142"
},
{
"value": ""
},
{
"value": ""
},
{
"value": ""
},
{
"value": ""
},
{
"value": ""
},
{
"value": ""
},
{
"value": ""
}
]
},
"Rows": {
"Row": [
{
"ColData": [
{
"value": "2016-03-16"
},
{
"value": "Invoice",
"id": "176"
},
{
"value": "1014"
},
{
"value": "Maxamillion Enterprises",
"id": "68"
},
{
"value": ""
},
{
"value": "Debtors",
"id": "140"
},
{
"value": "36.0"
},
{
"value": "36.0"
}
],
"type": "Data"
},
{
"ColData": [
{
"value": "2016-03-16"
},
{
"value": "Invoice",
"id": "179"
},
{
"value": "1015"
},
{
"value": "Hope Reality Limited",
"id": "69"
},
{
"value": ""
},
{
"value": "Debtors",
"id": "140"
},
{
"value": "18.0"
},
{
"value": "54.0"
}
],
"type": "Data"
},
{
"ColData": [
{
"value": "2016-03-17"
},
{
"value": "Bill",
"id": "185"
},
{
"value": ""
},
{
"value": "British Power",
"id": "72"
},
{
"value": ""
},
{
"value": "Creditors",
"id": "138"
},
{
"value": "-9.15"
},
{
"value": "44.85"
}
],
"type": "Data"
},
{
"ColData": [
{
"value": "2016-03-17"
},
{
"value": "Bill",
"id": "183"
},
{
"value": ""
},
{
"value": "Printing Ink Supplies",
"id": "71"
},
{
"value": ""
},
{
"value": "Creditors",
"id": "138"
},
{
"value": "-320.0"
},
{
"value": "-275.15"
}
],
"type": "Data"
},
{
"ColData": [
{
"value": "2016-03-17"
},
{
"value": "Bill",
"id": "178"
},
{
"value": ""
},
{
"value": "Teddy's T Shirt Supplier",
"id": "70"
},
{
"value": ""
},
{
"value": "Creditors",
"id": "138"
},
{
"value": "-120.0"
},
{
"value": "-395.15"
}
],
"type": "Data"
}
]
},
"Summary": {
"ColData": [
{
"value": "Total for VAT Control"
},
{
"value": ""
},
{
"value": ""
},
{
"value": ""
},
{
"value": ""
},
{
"value": ""
},
{
"value": "-395.15"
},
{
"value": ""
}
]
},
"type": "Section"
},
{
"Header": {
"ColData": [
{
"value": "Opening Balance Equity",
"id": "137"
},
{
"value": ""
},
{
"value": ""
},
{
"value": ""
},
{
"value": ""
},
{
"value": ""
},
{
"value": ""
},
{
"value": ""
}
]
},
"Rows": {
"Row": [
{
"ColData": [
{
"value": "2016-03-01"
},
{
"value": "Stock Starting Value",
"id": "173"
},
{
"value": "START"
},
{
"value": "",
"id": ""
},
{
"value": "Round Neck T Shirt - Opening stock and value"
},
{
"value": "Stock Asset",
"id": "136"
},
{
"value": "0.0"
},
{
"value": "0.0"
}
],
"type": "Data"
},
{
"ColData": [
{
"value": "2016-03-17"
},
{
"value": "Deposit",
"id": "180"
},
{
"value": ""
},
{
"value": "",
"id": ""
},
{
"value": ""
},
{
"value": "Current",
"id": "144"
},
{
"value": "2400.0"
},
{
"value": "2400.0"
}
],
"type": "Data"
}
]
},
"Summary": {
"ColData": [
{
"value": "Total for Opening Balance Equity"
},
{
"value": ""
},
{
"value": ""
},
{
"value": ""
},
{
"value": ""
},
{
"value": ""
},
{
"value": "2400.0"
},
{
"value": ""
}
]
},
"type": "Section"
},
{
"Header": {
"ColData": [
{
"value": "Sales of Product Income",
"id": "133"
},
{
"value": ""
},
{
"value": ""
},
{
"value": ""
},
{
"value": ""
},
{
"value": ""
},
{
"value": ""
},
{
"value": ""
}
]
},
"Rows": {
"Row": [
{
"ColData": [
{
"value": "2016-03-16"
},
{
"value": "Invoice",
"id": "179"
},
{
"value": "1015"
},
{
"value": "Hope Reality Limited",
"id": "69"
},
{
"value": "Round Neck T Shirt"
},
{
"value": "Debtors",
"id": "140"
},
{
"value": "60.0"
},
{
"value": "60.0"
}
],
"type": "Data"
},
{
"ColData": [
{
"value": "2016-03-16"
},
{
"value": "Invoice",
"id": "176"
},
{
"value": "1014"
},
{
"value": "Maxamillion Enterprises",
"id": "68"
},
{
"value": "Round Neck T Shirt"
},
{
"value": "Debtors",
"id": "140"
},
{
"value": "120.0"
},
{
"value": "180.0"
}
],
"type": "Data"
}
]
},
"Summary": {
"ColData": [
{
"value": "Total for Sales of Product Income"
},
{
"value": ""
},
{
"value": ""
},
{
"value": ""
},
{
"value": ""
},
{
"value": ""
},
{
"value": "180.0"
},
{
"value": ""
}
]
},
"type": "Section"
},
{
"Header": {
"ColData": [
{
"value": "Services",
"id": "131"
},
{
"value": ""
},
{
"value": ""
},
{
"value": ""
},
{
"value": ""
},
{
"value": ""
},
{
"value": ""
},
{
"value": ""
}
]
},
"Rows": {
"Row": [
{
"ColData": [
{
"value": "2016-03-16"
},
{
"value": "Invoice",
"id": "176"
},
{
"value": "1014"
},
{
"value": "Maxamillion Enterprises",
"id": "68"
},
{
"value": "Print on Pocket"
},
{
"value": "Debtors",
"id": "140"
},
{
"value": "60.0"
},
{
"value": "60.0"
}
],
"type": "Data"
},
{
"ColData": [
{
"value": "2016-03-16"
},
{
"value": "Invoice",
"id": "179"
},
{
"value": "1015"
},
{
"value": "Hope Reality Limited",
"id": "69"
},
{
"value": "Print on Pocket"
},
{
"value": "Debtors",
"id": "140"
},
{
"value": "30.0"
},
{
"value": "90.0"
}
],
"type": "Data"
}
]
},
"Summary": {
"ColData": [
{
"value": "Total for Services"
},
{
"value": ""
},
{
"value": ""
},
{
"value": ""
},
{
"value": ""
},
{
"value": ""
},
{
"value": "90.0"
},
{
"value": ""
}
]
},
"type": "Section"
},
{
"Header": {
"ColData": [
{
"value": "Cost of sales",
"id": "134"
},
{
"value": ""
},
{
"value": ""
},
{
"value": ""
},
{
"value": ""
},
{
"value": ""
},
{
"value": ""
},
{
"value": ""
}
]
},
"Rows": {
"Row": [
{
"ColData": [
{
"value": "2016-03-16"
},
{
"value": "Invoice",
"id": "176"
},
{
"value": "1014"
},
{
"value": "Maxamillion Enterprises",
"id": "68"
},
{
"value": "Round Neck T Shirt"
},
{
"value": "Debtors",
"id": "140"
},
{
"value": "24.0"
},
{
"value": "24.0"
}
],
"type": "Data"
},
{
"ColData": [
{
"value": "2016-03-16"
},
{
"value": "Invoice",
"id": "179"
},
{
"value": "1015"
},
{
"value": "Hope Reality Limited",
"id": "69"
},
{
"value": "Round Neck T Shirt"
},
{
"value": "Debtors",
"id": "140"
},
{
"value": "12.0"
},
{
"value": "36.0"
}
],
"type": "Data"
}
]
},
"Summary": {
"ColData": [
{
"value": "Total for Cost of sales"
},
{
"value": ""
},
{
"value": ""
},
{
"value": ""
},
{
"value": ""
},
{
"value": ""
},
{
"value": "36.0"
},
{
"value": ""
}
]
},
"type": "Section"
},
{
"Header": {
"ColData": [
{
"value": "Stock Shrinkage",
"id": "141"
},
{
"value": ""
},
{
"value": ""
},
{
"value": ""
},
{
"value": ""
},
{
"value": ""
},
{
"value": ""
},
{
"value": ""
}
]
}
etc. I had to cut this JSON short, limited to 30000 characters.
What have you tried so far? This seems pretty straightforward. To get the rows into an array format you would do something like the following:
var data = {
"Header": {
"Time": "2016-03-30T16:10:19-07:00",
"ReportName": "GeneralLedger",
"ReportBasis": "Accrual",
"StartPeriod": "2016-01-01",
"EndPeriod": "2016-03-31",
"Currency": "GBP",
"Option": [{
"Name": "NoReportData",
"Value": "false"
}]
},
"Columns": {
"Column": [{
"ColTitle": "Date",
"ColType": "tx_date"
}, {
"ColTitle": "Transaction Type",
"ColType": "txn_type"
}, {
"ColTitle": "No.",
"ColType": "doc_num"
}, {
"ColTitle": "Name",
"ColType": "name"
}, {
"ColTitle": "Memo/Description",
"ColType": "memo"
}, {
"ColTitle": "Split",
"ColType": "split_acc"
}, {
"ColTitle": "Amount",
"ColType": "subt_nat_amount"
}, {
"ColTitle": "Balance",
"ColType": "rbal_nat_amount"
}]
},
"Rows": {
"Row": [{
"Header": {
"ColData": [{
"value": "Current",
"id": "144"
}, {
"value": ""
}, {
"value": ""
}, {
"value": ""
}, {
"value": ""
}, {
"value": ""
}, {
"value": ""
}, {
"value": ""
}]
},
"Rows": {
"Row": [{
"ColData": [{
"value": "2016-03-16"
}, {
"value": "Bill Payment (Cheque)",
"id": "181"
}, {
"value": "1"
}, {
"value": "Teddy's T Shirt Supplier",
"id": "70"
}, {
"value": "104478"
}, {
"value": "Creditors",
"id": "138"
}, {
"value": "-600.0"
}, {
"value": "-600.0"
}],
"type": "Data"
}, {
"ColData": [{
"value": "2016-03-17"
}, {
"value": "Bill Payment (Cheque)",
"id": "184"
}, {
"value": "2"
}, {
"value": "Teddy's T Shirt Supplier",
"id": "70"
}, {
"value": "104478"
}, {
"value": "Creditors",
"id": "138"
}, {
"value": "-120.0"
}, {
"value": "-720.0"
}],
"type": "Data"
}, {
"ColData": [{
"value": "2016-03-17"
}, {
"value": "Deposit",
"id": "180"
}, {
"value": ""
}, {
"value": "",
"id": ""
}, {
"value": "Opening Balance"
}, {
"value": "Opening Balance Equity",
"id": "137"
}, {
"value": "2400.0"
}, {
"value": "1680.0"
}],
"type": "Data"
}, {
"ColData": [{
"value": "2016-03-23"
}, {
"value": "Payment",
"id": "186"
}, {
"value": "345678"
}, {
"value": "Maxamillion Enterprises",
"id": "68"
}, {
"value": ""
}, {
"value": "Debtors",
"id": "140"
}, {
"value": "216.0"
}, {
"value": "1896.0"
}],
"type": "Data"
}]
}
}]
}
};
function parse(data) {
var rows = [],
row, curRow, rowSegment;
for (var i = 0; i < data.Rows.Row.length; ++i) {
rowSegment = data.Rows.Row[i].Rows.Row;
for (var j = 0; j < rowSegment.length; ++j) {
row = [];
curRow = rowSegment[j].ColData;
for (var x = 0; x < curRow.length; ++x) {
row.push(curRow[x].value);
}
rows.push(row);
}
}
return rows;
}
var parsed = parse(data);
var rowEl, outEl = document.getElementById('html-out'),
val;
for (var i = 0; i < parsed.length; ++i) {
rowEl = document.createElement("div");
rowEl.setAttribute("class", "row");
rowEl.appendChild(document.createTextNode(parsed[i].join(', ')));
outEl.appendChild(rowEl);
}
<div id="html-out"></div>
Also, you would probably want to add the columns as the first row but this looks like it would get you the CSV-type data you are going for.
I have the following in a Bookshelf Model
create (data, options = {}) {
return this.forge(data)
.save(null, options);
},
findOne (data, options = {}) {
return this.forge(data).fetch(options);
},
findOrCreate (data, options = {}) {
let createOpts = _.clone(data);
let findOpts = _.pick(data, 'name', 'id');
return this.findOne(findOpts, options)
.then(model => {
return model ? model : this.create(createOpts, options);
});
}
I have a controller with a method that calls to the findOrCreate method like so:
function insertRows(ScenesCollection, Location) {
return config => {
const payload = config.payload;
const projectId = config.project.get('id');
const userId = config.userId;
let inserts = filterImportPayload(payload);
let inserted = [];
_.forEach(inserts, obj => {
let opts = extractOptions(obj, projectId);
let sceneOpts = _.omit(opts, 'location');
_.extend(sceneOpts, {
create_user: config.userId,
update_user: config.userId
});
if (obj.location) {
const locOpts = _.pick({
id: obj.location.value.id,
name: _.isObject(obj.location.value) ? undefined : obj.location.value,
project_id: projectId,
create_user: userId,
update_user: userId
}, _.identity);
inserted.push(Location.findOrCreate(locOpts).then(location => {
sceneOpts.location_id = location.get('id');
}).thenReturn(sceneOpts));
} else {
inserted.push(sceneOpts);
}
});
return Bluebird.all(inserted).then(inserts => {
config.inserted = ScenesCollection.forge(inserts).invokeThen('save');
return Bluebird.props(config);
});
}
}
And the payload that comes in from the POST request is
[{
"name": {
"value": "2a",
"type": "data"
},
"slugline": {
"value": "The Linten Bakery.",
"type": "data"
},
"description": {
"value": "Mary is a hastily prepared but surprisingly breakfast. :)",
"type": "data"
},
"story_day": {
"value": 2,
"type": "data"
},
"page_count": {
"value": 1.5,
"type": "data"
},
"location": {
"type": "matched",
"value": {
"id": 149
}
},
"day_night": {
"type": "matched",
"value": {
"id": 10
}
},
"int_ext": {
"type": "matched",
"value": {
"id": 26
}
},
"extras_count": {
"value": 3,
"type": "data"
},
"type": "insert"
}, {
"name": {
"value": "PU 11a",
"type": "data"
},
"slugline": {
"value": "Office. Not good. But worth it.asd",
"type": "data"
},
"description": {
"value": "asd",
"type": "data"
},
"story_day": {
"value": 1,
"type": "data"
},
"page_count": {
"value": 1,
"type": "data"
},
"location": {
"type": "matched",
"value": {
"id": 149
}
},
"day_night": {
"type": "matched",
"value": {
"id": 10
}
},
"int_ext": {
"value": null,
"type": "ignore"
},
"extras_count": {
"value": 16,
"type": "data"
},
"type": "insert"
}, {
"name": {
"value": "PU 1",
"type": "data"
},
"slugline": {
"value": "Hallwayadqwe",
"type": "data"
},
"description": {
"value": "Mary goes to the kitchennettie.ads",
"type": "data"
},
"story_day": {
"value": 1,
"type": "data"
},
"page_count": {
"value": 1.75,
"type": "data"
},
"location": {
"type": "matched",
"value": {
"id": 149
}
},
"day_night": {
"type": "matched",
"value": {
"id": 10
}
},
"int_ext": {
"type": "matched",
"value": {
"id": 26
}
},
"extras_count": {
"value": 1,
"type": "data"
},
"type": "insert"
}, {
"name": {
"value": "PU 11b",
"type": "data"
},
"slugline": {
"value": "Officeqe",
"type": "data"
},
"description": {
"value": "Extras enter office, causing commotionads",
"type": "data"
},
"story_day": {
"value": 1,
"type": "data"
},
"page_count": {
"value": 0.5,
"type": "data"
},
"location": {
"value": "The Cinema",
"type": "insert"
},
"day_night": {
"type": "matched",
"value": {
"id": 10
}
},
"int_ext": {
"type": "matched",
"value": {
"id": 26
}
},
"extras_count": {
"value": 15,
"type": "data"
},
"type": "insert"
}, {
"name": {
"value": "443",
"type": "data"
},
"slugline": {
"value": "Office",
"type": "data"
},
"description": {
"value": "Wide of Mary standing in what remains of the officeasd",
"type": "data"
},
"story_day": {
"value": "",
"type": "ignore"
},
"page_count": {
"value": 1,
"type": "data"
},
"location": {
"value": "Milwaukee General Hospital",
"type": "insert"
},
"day_night": {
"type": "matched",
"value": {
"id": 10
}
},
"int_ext": {
"type": "matched",
"value": {
"id": 26
}
},
"extras_count": {
"value": 0,
"type": "ignore"
},
"type": "insert"
}, {
"name": {
"value": "PU 6",
"type": "data"
},
"slugline": {
"value": "Office",
"type": "data"
},
"description": {
"value": "Mary walks up to office, out of breath. asd",
"type": "data"
},
"story_day": {
"value": 1,
"type": "data"
},
"page_count": {
"value": 0.5,
"type": "data"
},
"location": {
"value": "My Room",
"type": "insert"
},
"day_night": {
"type": "matched",
"value": {
"id": 10
}
},
"int_ext": {
"type": "matched",
"value": {
"id": 26
}
},
"extras_count": {
"value": "",
"type": "ignore"
},
"type": "insert"
}, {
"name": {
"value": "PU 9",
"type": "data"
},
"slugline": {
"value": "Office/radio",
"type": "data"
},
"description": {
"value": "Mary enjoying listening to her radio at a reasonable volume.ads",
"type": "data"
},
"story_day": {
"value": 1,
"type": "data"
},
"page_count": {
"value": 0.5,
"type": "data"
},
"location": {
"value": "My Room",
"type": "insert"
},
"day_night": {
"type": "matched",
"value": {
"id": 10
}
},
"int_ext": {
"type": "matched",
"value": {
"id": 26
}
},
"type": "insert"
}, {
"name": {
"value": "PU 8b",
"type": "data"
},
"slugline": {
"value": "Office",
"type": "data"
},
"description": {
"value": "Mary converses with Alice outside her officead",
"type": "data"
},
"story_day": {
"value": 1,
"type": "data"
},
"page_count": {
"value": 2.5,
"type": "data"
},
"location": {
"value": "The Cinema",
"type": "insert"
},
"day_night": {
"type": "matched",
"value": {
"id": 10
}
},
"int_ext": {
"type": "matched",
"value": {
"id": 26
}
},
"type": "insert"
}, {
"name": {
"value": "PU 5a",
"type": "data"
},
"slugline": {
"value": "Street",
"type": "data"
},
"description": {
"value": "Wide of mary and child with ballasd",
"type": "data"
},
"story_day": {
"value": 1,
"type": "data"
},
"page_count": {
"value": 1,
"type": "data"
},
"location": {
"value": "My Room",
"type": "insert"
},
"day_night": {
"type": "matched",
"value": {
"id": 11
}
},
"int_ext": {
"type": "matched",
"value": {
"id": 26
}
},
"extras_count": {
"value": 1,
"type": "data"
},
"type": "insert"
}, {
"name": {
"value": "3a",
"type": "data"
},
"slugline": {
"value": "Outside Door",
"type": "data"
},
"description": {
"value": "Mary exits the house",
"type": "data"
},
"story_day": {
"value": 1,
"type": "data"
},
"page_count": {
"value": 0.5,
"type": "data"
},
"location": {
"type": "matched",
"value": {
"id": 491
}
},
"day_night": {
"type": "matched",
"value": {
"id": 10
}
},
"int_ext": {
"type": "matched",
"value": {
"id": 26
}
},
"extras_count": {
"value": 0,
"type": "ignore"
},
"type": "insert"
}, {
"name": {
"value": "2b",
"type": "data"
},
"slugline": {
"value": "Mary's Kitchen",
"type": "data"
},
"description": {
"value": "Mary begins to prepare a second breakfast.",
"type": "data"
},
"story_day": {
"value": 1,
"type": "data"
},
"page_count": {
"value": 1.125,
"type": "data"
},
"day_night": {
"type": "matched",
"value": {
"id": 10
}
},
"int_ext": {
"type": "matched",
"value": {
"id": 27
}
},
"extras_count": {
"value": 1,
"type": "data"
},
"type": "insert"
}, {
"name": {
"value": "83",
"type": "data"
},
"slugline": {
"value": "Office",
"type": "data"
},
"description": {
"value": "Carsten enters Mary's office, informs her she will need to relocate downstairs.",
"type": "data"
},
"story_day": {
"value": 1,
"type": "data"
},
"page_count": {
"value": 0.5,
"type": "data"
},
"location": {
"value": "My Room",
"type": "insert"
},
"day_night": {
"type": "matched",
"value": {
"id": 10
}
},
"int_ext": {
"type": "matched",
"value": {
"id": 26
}
},
"extras_count": {
"value": 0,
"type": "ignore"
},
"type": "insert"
}, {
"name": {
"value": "333",
"type": "data"
},
"slugline": {
"value": "COLLISIUM WEST",
"type": "data"
},
"description": {
"value": "There is a large explosion and then people start running out of the building",
"type": "data"
},
"story_day": {
"value": 10,
"type": "data"
},
"page_count": {
"value": 0,
"type": "ignore"
},
"location": {
"value": "Milwaukee General Hospital",
"type": "insert"
},
"day_night": {
"value": null,
"type": "ignore"
},
"int_ext": {
"type": "matched",
"value": {
"id": 27
}
},
"extras_count": {
"value": 12,
"type": "data"
},
"type": "insert"
}, {
"name": {
"value": "43",
"type": "data"
},
"slugline": {
"value": "I love THAT",
"type": "data"
},
"description": {
"value": "bbg",
"type": "data"
},
"story_day": {
"value": 12,
"type": "data"
},
"page_count": {
"value": 0,
"type": "ignore"
},
"location": {
"value": "Milwaukee General Hospital",
"type": "insert"
},
"day_night": {
"type": "matched",
"value": {
"id": 11
}
},
"int_ext": {
"type": "matched",
"value": {
"id": 27
}
},
"extras_count": {
"value": 10,
"type": "data"
},
"type": "insert"
}]
The problem I am having is that the findOrCreate method is creating multiple records of the same location.