JSON (from HTML Table) to CSV - json

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.

Related

JMeter jp#gc JSON/YAML Path Assertion for an array

I'm trying to use jp#gc - JSON/YAML Path Assertion, when my Response Body is:
[
{
"label": "Alabama",
"value": "AL"
},
{
"label": "Alaska",
"value": "AK"
},
{
"label": "American Samoa",
"value": "AS"
},
{
"label": "Arizona",
"value": "AZ"
},
{
"label": "Arkansas",
"value": "AR"
},
{
"label": "California",
"value": "CA"
},
{
"label": "Colorado",
"value": "CO"
},
{
"label": "Connecticut",
"value": "CT"
},
{
"label": "Delaware",
"value": "DE"
},
{
"label": "District Of Columbia",
"value": "DC"
},
{
"label": "Federated States Of Micronesia",
"value": "FM"
},
{
"label": "Florida",
"value": "FL"
},
{
"label": "Georgia",
"value": "GA"
},
{
"label": "Guam",
"value": "GU"
},
{
"label": "Hawaii",
"value": "HI"
},
{
"label": "Idaho",
"value": "ID"
},
{
"label": "Illinois",
"value": "IL"
},
{
"label": "Indiana",
"value": "IN"
},
{
"label": "Iowa",
"value": "IA"
},
{
"label": "Kansas",
"value": "KS"
},
{
"label": "Kentucky",
"value": "KY"
},
{
"label": "Louisiana",
"value": "LA"
},
{
"label": "Maine",
"value": "ME"
},
{
"label": "Marshall Islands",
"value": "MH"
},
{
"label": "Maryland",
"value": "MD"
},
{
"label": "Massachusetts",
"value": "MA"
},
{
"label": "Michigan",
"value": "MI"
},
{
"label": "Minnesota",
"value": "MN"
},
{
"label": "Mississippi",
"value": "MS"
},
{
"label": "Missouri",
"value": "MO"
},
{
"label": "Montana",
"value": "MT"
},
{
"label": "Nebraska",
"value": "NE"
},
{
"label": "Nevada",
"value": "NV"
},
{
"label": "New Hampshire",
"value": "NH"
},
{
"label": "New Jersey",
"value": "NJ"
},
{
"label": "New Mexico",
"value": "NM"
},
{
"label": "New York",
"value": "NY"
},
{
"label": "North Carolina",
"value": "NC"
},
{
"label": "North Dakota",
"value": "ND"
},
{
"label": "Northern Mariana Islands",
"value": "MP"
},
{
"label": "Ohio",
"value": "OH"
},
{
"label": "Oklahoma",
"value": "OK"
},
{
"label": "Oregon",
"value": "OR"
},
{
"label": "Palau",
"value": "PW"
},
{
"label": "Pennsylvania",
"value": "PA"
},
{
"label": "Puerto Rico",
"value": "PR"
},
{
"label": "Rhode Island",
"value": "RI"
},
{
"label": "South Carolina",
"value": "SC"
},
{
"label": "South Dakota",
"value": "SD"
},
{
"label": "Tennessee",
"value": "TN"
},
{
"label": "Texas",
"value": "TX"
},
{
"label": "Utah",
"value": "UT"
},
{
"label": "Vermont",
"value": "VT"
},
{
"label": "Virgin Islands",
"value": "VI"
},
{
"label": "Virginia",
"value": "VA"
},
{
"label": "Washington",
"value": "WA"
},
{
"label": "West Virginia",
"value": "WV"
},
{
"label": "Wisconsin",
"value": "WI"
},
{
"label": "Wyoming",
"value": "WY"
}
]
Here is how is use the JSON/YAML Path Assertion:
but I'm getting Assertion failure:
The following setup should work for you:
More information:
JsonPath - Getting Started
Perl 5 Regex Cheat sheet
The New JSON/YAML Plugin - Using YAML in JMeter
Use JSON Assertion ( jp#gc - JSON/YAML is deprecated)
JSON start with array so use JSON Path
$.[0].label
You can also uncheck Match as regular expression

How to extract the path info?

For the following JSON, I'd like to extract something like this ( is a TAB character).
CHROMOSOMES<TAB>HUMAN<TAB>1<TAB>1
...
STATUSES<TAB>name<TAB>Approved
...
ATTRIBUTES<TAB>HGNC<TAB>HGNC ID<TAB>gd_hgnc_id
...
ATTRIBUTES<TAB>EXTERNAL<TAB>NCBI Gene ID<TAB>md_eg_id<TAB>NCBI
...
ORDER_BY<TAB>HGNC ID<TAB>gd_hgnc_id
...
I'd like a smart way to extract the path info of this tree structure. Could you anybody show me the best way to do so? Thanks.
{
"CHROMOSOMES": {
"HUMAN": [
{
"name": "1",
"value": "1"
},
{
"name": "2",
"value": "2"
},
{
"name": "3",
"value": "3"
},
{
"name": "4",
"value": "4"
},
{
"name": "5",
"value": "5"
},
{
"name": "6",
"value": "6"
},
{
"name": "7",
"value": "7"
},
{
"name": "8",
"value": "8"
},
{
"name": "9",
"value": "9"
},
{
"name": "10",
"value": "10"
},
{
"name": "11",
"value": "11"
},
{
"name": "12",
"value": "12"
},
{
"name": "13",
"value": "13"
},
{
"name": "14",
"value": "14"
},
{
"name": "15",
"value": "15"
},
{
"name": "16",
"value": "16"
},
{
"name": "17",
"value": "17"
},
{
"name": "18",
"value": "18"
},
{
"name": "19",
"value": "19"
},
{
"name": "20",
"value": "20"
},
{
"name": "21",
"value": "21"
},
{
"name": "22",
"value": "22"
},
{
"name": "X",
"value": "X"
},
{
"name": "Y",
"value": "Y"
},
{
"name": "reserved loci",
"value": "reserved"
},
{
"name": "mitochondrial",
"value": "mito"
},
{
"name": "pseudoautosomal",
"value": "XandY"
}
]
},
"STATUSES": [
{
"name": "Approved",
"value": "Approved"
},
{
"name": "Entry and symbol withdrawn",
"value": "Entry Withdrawn"
}
],
"ATTRIBUTES": {
"HGNC": [
{
"name": "HGNC ID",
"value": "gd_hgnc_id"
},
{
"name": "Approved symbol",
"value": "gd_app_sym"
},
{
"name": "Approved name",
"value": "gd_app_name"
},
{
"name": "Status",
"value": "gd_status"
},
{
"name": "Locus type",
"value": "gd_locus_type"
},
{
"name": "Locus group",
"value": "gd_locus_group"
},
{
"name": "Previous symbols",
"value": "gd_prev_sym"
},
{
"name": "Previous name",
"value": "gd_prev_name"
},
{
"name": "Synonyms",
"value": "gd_aliases"
},
{
"name": "Name synonyms",
"value": "gd_name_aliases"
},
{
"name": "Chromosome",
"value": "gd_pub_chrom_map"
},
{
"name": "Date approved",
"value": "gd_date2app_or_res"
},
{
"name": "Date modified",
"value": "gd_date_mod"
},
{
"name": "Date symbol changed",
"value": "gd_date_sym_change"
},
{
"name": "Date name changed",
"value": "gd_date_name_change"
},
{
"name": "Accession numbers",
"value": "gd_pub_acc_ids"
},
{
"name": "Enzyme IDs",
"value": "gd_enz_ids"
},
{
"name": "NCBI Gene ID",
"value": "gd_pub_eg_id"
},
{
"name": "Ensembl gene ID",
"value": "gd_pub_ensembl_id"
},
{
"name": "Mouse genome database ID",
"value": "gd_mgd_id"
},
{
"name": "Specialist database links",
"value": "gd_other_ids"
},
{
"name": "Specialist database IDs",
"value": "gd_other_ids_list"
},
{
"name": "Pubmed IDs",
"value": "gd_pubmed_ids"
},
{
"name": "RefSeq IDs",
"value": "gd_pub_refseq_ids"
},
{
"name": "Gene group ID",
"value": "family.id"
},
{
"name": "Gene group name",
"value": "family.name"
},
{
"name": "CCDS IDs",
"value": "gd_ccds_ids"
},
{
"name": "Vega IDs",
"value": "gd_vega_ids"
},
{
"name": "Locus specific databases",
"value": "gd_lsdb_links"
}
],
"EXTERNAL": [
{
"name": "NCBI Gene ID",
"source": "NCBI",
"value": "md_eg_id"
},
{
"name": "OMIM ID",
"source": "OMIM",
"value": "md_mim_id"
},
{
"name": "RefSeq",
"source": "NCBI",
"value": "md_refseq_id"
},
{
"name": "UniProt ID",
"source": "UniProt",
"value": "md_prot_id"
},
{
"name": "Ensembl ID",
"source": "Ensembl",
"value": "md_ensembl_id"
},
{
"name": "Vega ID",
"source": "Vega",
"value": "md_vega_id"
},
{
"name": "UCSC ID",
"source": "UCSC",
"value": "md_ucsc_id"
},
{
"name": "Mouse genome database ID",
"source": "MGI",
"value": "md_mgd_id"
},
{
"name": "Rat genome database ID",
"source": "RGD",
"value": "md_rgd_id"
},
{
"name": "LNCipedia",
"source": "LNCipedia",
"value": "md_lncipedia"
},
{
"name": "GtRNAdb",
"source": "GtRNAdb",
"value": "md_gtrnadb"
}
]
},
"ORDER_BY": [
{
"name": "HGNC ID",
"value": "gd_hgnc_id"
},
{
"name": "Approved symbol",
"value": "gd_app_sym_sort"
},
{
"name": "Approved name",
"value": "gd_app_name"
},
{
"name": "Status",
"value": "gd_status"
},
{
"name": "Locus type",
"value": "gd_locus_type"
},
{
"name": "Locus group",
"value": "gd_locus_group"
},
{
"name": "Previous symbols",
"value": "gd_prev_sym"
},
{
"name": "Previous name",
"value": "gd_prev_name"
},
{
"name": "Synonyms",
"value": "gd_aliases"
},
{
"name": "Name synonyms",
"value": "gd_name_aliases"
},
{
"name": "Chromosome",
"value": "gd_pub_chrom_map_sort"
},
{
"name": "Date approved",
"value": "gd_date2app_or_res"
},
{
"name": "Date modified",
"value": "gd_date_mod"
},
{
"name": "Date symbol changed",
"value": "gd_date_sym_change"
},
{
"name": "Date name changed",
"value": "gd_date_name_change"
},
{
"name": "Accession numbers",
"value": "gd_pub_acc_ids"
},
{
"name": "Enzyme IDs",
"value": "gd_enz_ids"
},
{
"name": "NCBI Gene ID",
"value": "gd_pub_eg_id"
},
{
"name": "Ensembl gene ID",
"value": "gd_pub_ensembl_id"
},
{
"name": "Mouse genome database ID",
"value": "gd_mgd_id"
},
{
"name": "Specialist database links",
"value": "gd_other_ids"
},
{
"name": "Specialist database IDs",
"value": "gd_other_ids_list"
},
{
"name": "Pubmed IDs",
"value": "gd_pubmed_ids"
},
{
"name": "RefSeq IDs",
"value": "gd_pub_refseq_ids"
},
{
"name": "Gene group ID",
"value": "family.id"
},
{
"name": "Gene group name",
"value": "family.name"
},
{
"name": "CCDS IDs",
"value": "gd_ccds_ids"
},
{
"name": "Vega IDs",
"value": "gd_vega_ids"
},
{
"name": "Locus specific databases",
"value": "gd_lsdb_links"
},
{
"name": "NCBI Gene ID (supplied by NCBI)",
"value": "md_eg_id"
},
{
"name": "OMIM ID (supplied by OMIM)",
"value": "md_mim_id"
},
{
"name": "RefSeq (supplied by NCBI)",
"value": "md_refseq_id"
},
{
"name": "UniProt ID (supplied by UniProt)",
"value": "md_prot_id"
},
{
"name": "Ensembl ID (supplied by Ensembl)",
"value": "md_ensembl_id"
},
{
"name": "Vega ID (supplied by Vega)",
"value": "md_vega_id"
},
{
"name": "UCSC ID (supplied by UCSC)",
"value": "md_ucsc_id"
},
{
"name": "Mouse genome database ID (supplied by MGI)",
"value": "md_mgd_id"
},
{
"name": "Rat genome database ID (supplied by RGD)",
"value": "md_rgd_id"
},
{
"name": "LNCipedia ID (supplied by LNCipedia)",
"value": "md_lncipedia"
},
{
"name": "GtRNAdb ID (supplied by GtRNAdb)",
"value": "md_gtrnadb"
}
],
"OUTPUT": [
"Text",
"Make URL for text"
]
}
I'd like a smart way to extract the path info of this tree structure.
paths is your friend.
Given certain irregularities in the input, the exact requirements are
not always clear, but the following might be what you are looking for
and even if not, it would be easy to tweak in accordance with your
detailed requirements.
totsv.jq
def s: map(select(type=="string"));
paths as $p
| getpath($p)
| if type == "object" and has("name")
then ($p|s) + [.name, .value, (.source // empty)]
elif type == "array" and .[0] == "Text" then ($p|s) + .
else empty
end
| #tsv
Invocation
jq -crf totsv.jq chromosomes.json
Selection from output
CHROMOSOMES HUMAN 1 1
CHROMOSOMES HUMAN 2 2
...
STATUSES Approved Approved
STATUSES Entry and symbol withdrawn Entry Withdrawn
ATTRIBUTES HGNC HGNC ID gd_hgnc_id
...
ORDER_BY GtRNAdb ID (supplied by GtRNAdb) md_gtrnadb
OUTPUT Text Make URL for text
For future reference
Rather than give a very long sample input, it would be better
to give a small sample that is tightly woven with detailed requirements.

How to get a value of a object from its foreign key?

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

Bookshelf.js FindOrCreate method

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.

Best way to flatten json

I have this awful looking JSON that is returned from the SugarCRM API.
What's the best approach to clean it up and remove the "name" key, and just have a single key and value (with no nested objects)
I wouldn't mind using the underscore library if needed.
{
"assigned_user_name": {
"name": "assigned_user_name",
"value": ""
},
"modified_by_name": {
"name": "modified_by_name",
"value": "Website Administrator"
},
"created_by_name": {
"name": "created_by_name",
"value": "Website Administrator"
},
"id": {
"name": "id",
"value": "6f9ec13f-dc29-ff18-da36-52d81a0076ad"
},
"name": {
"name": "name",
"value": " "
},
"date_entered": {
"name": "date_entered",
"value": "2014-01-16 17:45:33"
},
"date_modified": {
"name": "date_modified",
"value": "2014-01-16 17:45:33"
},
"modified_user_id": {
"name": "modified_user_id",
"value": "ab5ff74f-8043-f125-1409-523b6767fca9"
},
"created_by": {
"name": "created_by",
"value": "ab5ff74f-8043-f125-1409-523b6767fca9"
},
"description": {
"name": "description",
"value": ""
},
"deleted": {
"name": "deleted",
"value": "0"
},
"assigned_user_id": {
"name": "assigned_user_id",
"value": ""
},
"salutation": {
"name": "salutation",
"value": ""
},
"first_name": {
"name": "first_name",
"value": ""
},
"last_name": {
"name": "last_name",
"value": ""
},
"full_name": {
"name": "full_name",
"value": " "
},
"title": {
"name": "title",
"value": ""
},
"department": {
"name": "department",
"value": ""
},
"do_not_call": {
"name": "do_not_call",
"value": "0"
},
"phone_home": {
"name": "phone_home",
"value": ""
},
"email": {
"name": "email",
"value": ""
},
"phone_mobile": {
"name": "phone_mobile",
"value": ""
},
"phone_work": {
"name": "phone_work",
"value": ""
},
"phone_other": {
"name": "phone_other",
"value": ""
},
"phone_fax": {
"name": "phone_fax",
"value": ""
},
"email1": {
"name": "email1",
"value": "test#example.com"
},
"email2": {
"name": "email2",
"value": ""
},
"invalid_email": {
"name": "invalid_email",
"value": "0"
},
"email_opt_out": {
"name": "email_opt_out",
"value": "0"
},
"primary_address_street": {
"name": "primary_address_street",
"value": ""
},
"primary_address_street_2": {
"name": "primary_address_street_2",
"value": ""
},
"primary_address_street_3": {
"name": "primary_address_street_3",
"value": ""
},
"primary_address_city": {
"name": "primary_address_city",
"value": ""
},
"primary_address_state": {
"name": "primary_address_state",
"value": ""
},
"primary_address_postalcode": {
"name": "primary_address_postalcode",
"value": ""
},
"primary_address_country": {
"name": "primary_address_country",
"value": ""
},
"alt_address_street": {
"name": "alt_address_street",
"value": ""
},
"alt_address_street_2": {
"name": "alt_address_street_2",
"value": ""
},
"alt_address_street_3": {
"name": "alt_address_street_3",
"value": ""
},
"alt_address_city": {
"name": "alt_address_city",
"value": ""
},
"alt_address_state": {
"name": "alt_address_state",
"value": ""
},
"alt_address_postalcode": {
"name": "alt_address_postalcode",
"value": ""
},
"alt_address_country": {
"name": "alt_address_country",
"value": ""
},
"assistant": {
"name": "assistant",
"value": ""
},
"assistant_phone": {
"name": "assistant_phone",
"value": ""
},
"email_and_name1": {
"name": "email_and_name1",
"value": ""
},
"lead_source": {
"name": "lead_source",
"value": ""
},
"opportunity_role_fields": {
"name": "opportunity_role_fields",
"value": ""
},
"opportunity_role_id": {
"name": "opportunity_role_id",
"value": ""
},
"opportunity_role": {
"name": "opportunity_role",
"value": ""
},
"reports_to_id": {
"name": "reports_to_id",
"value": ""
},
"report_to_name": {
"name": "report_to_name",
"value": ""
},
"birthdate": {
"name": "birthdate",
"value": false
},
"campaign_id": {
"name": "campaign_id",
"value": ""
},
"campaign_name": {
"name": "campaign_name",
"value": ""
},
"c_accept_status_fields": {
"name": "c_accept_status_fields",
"value": ""
},
"m_accept_status_fields": {
"name": "m_accept_status_fields",
"value": ""
},
"accept_status_id": {
"name": "accept_status_id",
"value": ""
},
"accept_status_name": {
"name": "accept_status_name",
"value": ""
},
"sync_contact": {
"name": "sync_contact",
"value": ""
},
"event_contact_fields": {
"name": "event_contact_fields",
"value": ""
},
"event_contact_id": {
"name": "event_contact_id",
"value": ""
},
"event_status": {
"name": "event_status",
"value": ""
},
"cc_sync": {
"name": "cc_sync",
"value": "0"
},
"cc_id": {
"name": "cc_id",
"value": ""
},
"cc_lists": {
"name": "cc_lists",
"value": ""
},
"cc_lists_view": {
"name": "cc_lists_view",
"value": ""
},
"cc_optout": {
"name": "cc_optout",
"value": "0"
},
"mc_expires_c": {
"name": "mc_expires_c",
"value": false
},
"birthyear_c": {
"name": "birthyear_c",
"value": ""
},
"contact_id_c": {
"name": "contact_id_c",
"value": ""
},
"currency_id": {
"name": "currency_id",
"value": ""
},
"employer_c": {
"name": "employer_c",
"value": ""
},
"employer_website_c": {
"name": "employer_website_c",
"value": ""
},
"fico_score_c": {
"name": "fico_score_c",
"value": ""
},
"household_income_c": {
"name": "household_income_c",
"value": ""
},
"investment_capital_c": {
"name": "investment_capital_c",
"value": ""
},
"job_title_c": {
"name": "job_title_c",
"value": ""
},
"mbrs_company_c": {
"name": "mbrs_company_c",
"value": ""
},
"membr_type_c": {
"name": "membr_type_c",
"value": ""
},
"nickname_c": {
"name": "nickname_c",
"value": ""
},
"num_properties_financed_c": {
"name": "num_properties_financed_c",
"value": ""
},
"num_properties_owned_c": {
"name": "num_properties_owned_c",
"value": ""
},
"own_primary_residence_c": {
"name": "own_primary_residence_c",
"value": ""
},
"parent_id": {
"name": "parent_id",
"value": ""
},
"parent_name": {
"name": "parent_name",
"value": ""
},
"parent_type": {
"name": "parent_type",
"value": ""
},
"planned_retirement_date_c": {
"name": "planned_retirement_date_c",
"value": false
},
"realestateadvisor_c": {
"name": "realestateadvisor_c",
"value": ""
},
"referral_c": {
"name": "referral_c",
"value": ""
},
"sdira_c": {
"name": "sdira_c",
"value": ""
},
"self_directed_ira_c": {
"name": "self_directed_ira_c",
"value": ""
},
"self_directed_ira_capital_c": {
"name": "self_directed_ira_capital_c",
"value": ""
},
"source_c": {
"name": "source_c",
"value": ""
},
"source_sub_c": {
"name": "source_sub_c",
"value": ""
},
"spouse_c": {
"name": "spouse_c",
"value": ""
},
"user_id1_c": {
"name": "user_id1_c",
"value": ""
},
"user_id_c": {
"name": "user_id_c",
"value": ""
},
"usigndate_c": {
"name": "usigndate_c",
"value": ""
},
"usign_c": {
"name": "usign_c",
"value": ""
},
"longitude_c": {
"name": "longitude_c",
"value": ""
},
"latitude_c": {
"name": "latitude_c",
"value": ""
},
"newmemberform_c": {
"name": "newmemberform_c",
"value": ""
},
"nr_contact_lead_source_c": {
"name": "nr_contact_lead_source_c",
"value": ""
},
"nr_contact_lead_score_c": {
"name": "nr_contact_lead_score_c",
"value": ""
},
"nr_contact_recent_activity_c": {
"name": "nr_contact_recent_activity_c",
"value": ""
}
}
If you want to use Underscore then reduce will do the trick:
var o = _(ugly).reduce(function(m, h) {
m[h.name] = h.value;
return m;
}, { });
Demo: http://jsfiddle.net/ambiguous/mE56S/
I'd just use a straight forward approach:
var newArr = {};
for(var key in YOUR_JSON_ARRAY)
newArr[key] = YOUR_JSON_ARRAY[key].value;
Keep in mind that the convention in the vardef array is to have the array indices be the field names, but it's not mandatory. I can't think of an example where they're not the same, but you could create a field with an array like 'my_new_field' => array('name'=>'new_field_c') and it would work.