Skip to content

Instantly share code, notes, and snippets.

@ultimatemonty
Created October 3, 2014 21:21
Show Gist options
  • Save ultimatemonty/2af7b62f302d674760e4 to your computer and use it in GitHub Desktop.
Save ultimatemonty/2af7b62f302d674760e4 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
<meta charset="utf-8">
<title>JS Bin</title>
<style id="jsbin-css">
pre {outline: 1px solid #ccc; padding: 5px; margin: 5px; }
.string { color: green; }
.number { color: darkorange; }
.boolean { color: blue; }
.null { color: magenta; }
.key { color: red; }
</style>
</head>
<body>
<script id="jsbin-javascript">
function output(inp) {
document.body.appendChild(document.createElement('pre')).innerHTML = inp;
}
// pretty-print the JSON
function syntaxHighlight(json) {
json = json.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
return json.replace(/("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)/g, function (match) {
var cls = 'number';
if (/^"/.test(match)) {
if (/:$/.test(match)) {
cls = 'key';
} else {
cls = 'string';
}
} else if (/true|false/.test(match)) {
cls = 'boolean';
} else if (/null/.test(match)) {
cls = 'null';
}
return '<span class="' + cls + '">' + match + '</span>';
});
}
function transform(payload) {
// get the record
var record = payload.record[0];
// get the vendorTypeIds into their own var and remove from the original record
var vendorTypeIds = record.VendorsVendorTypes_by_vendorId;
delete record.VendorsVendorTypes_by_vendorId;
// get the vendorTypes into their own var and remove from the original record
var vendorTypes = record.VendorTypes_by_VendorsVendorTypes;
delete record.VendorTypes_by_VendorsVendorTypes;
// Map the vendorTypeIds to record.vendorTypes to get the array ED wants
record.vendorTypes = vendorTypeIds.map(function (item) {
return item.id;
});
// reset the record
payload.record = record;
// Sideload the vendorTypes array to the payload
payload.vendorTypes = vendorTypes;
// BOOM
return payload;
}
var payload = {
record: [
{
id: 2,
name: "3M Aerospace",
website: "http://solutions.3m.com/wps/portal/3M/en_US/Aerospace/Aircraft/",
isActive: true,
isQAActive: true,
createdBy: "32",
createdDate: "2014-10-01T15:19:35",
modifiedBy: null,
modifiedDate: null,
VendorsVendorTypes_by_vendorId: [
{
id: 2,
vendorId: 2,
vendorTypeId: 1
},
{
id: 3,
vendorId: 2,
vendorTypeId: 2
},
{
id: 4,
vendorId: 2,
vendorTypeId: 3
}
],
VendorTypes_by_VendorsVendorTypes: [
{
id: 1,
type: "Component Repair",
isActive: true,
createdBy: "32",
createdDate: "2014-09-30T14:05:34",
modifiedBy: "32",
modifiedDate: "2014-09-30T14:15:39"
},
{
id: 2,
type: "Parts Supplier",
isActive: true,
createdBy: "32",
createdDate: "2014-09-30T14:34:21",
modifiedBy: null,
modifiedDate: null
},
{
id: 3,
type: "Materials Supplier",
isActive: true,
createdBy: "32",
createdDate: "2014-09-30T14:35:21",
modifiedBy: "32",
modifiedDate: "2014-09-30T14:35:48"
}
]
}
]
}
var original = JSON.stringify(payload, undefined, 4);
var transformed = JSON.stringify(transform(payload), undefined, 4);
output(syntaxHighlight(original));
output(syntaxHighlight(transformed));
</script>
<script id="jsbin-source-html" type="text/html"><!DOCTYPE html>
<html>
<head>
<script src="//code.jquery.com/jquery-2.1.1.min.js"><\/script>
<meta charset="utf-8">
<title>JS Bin</title>
</head>
<body>
</body>
</html></script>
<script id="jsbin-source-css" type="text/css">pre {outline: 1px solid #ccc; padding: 5px; margin: 5px; }
.string { color: green; }
.number { color: darkorange; }
.boolean { color: blue; }
.null { color: magenta; }
.key { color: red; }</script>
<script id="jsbin-source-javascript" type="text/javascript">function output(inp) {
document.body.appendChild(document.createElement('pre')).innerHTML = inp;
}
// pretty-print the JSON
function syntaxHighlight(json) {
json = json.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
return json.replace(/("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)/g, function (match) {
var cls = 'number';
if (/^"/.test(match)) {
if (/:$/.test(match)) {
cls = 'key';
} else {
cls = 'string';
}
} else if (/true|false/.test(match)) {
cls = 'boolean';
} else if (/null/.test(match)) {
cls = 'null';
}
return '<span class="' + cls + '">' + match + '</span>';
});
}
function transform(payload) {
// get the record
var record = payload.record[0];
// get the vendorTypeIds into their own var and remove from the original record
var vendorTypeIds = record.VendorsVendorTypes_by_vendorId;
delete record.VendorsVendorTypes_by_vendorId;
// get the vendorTypes into their own var and remove from the original record
var vendorTypes = record.VendorTypes_by_VendorsVendorTypes;
delete record.VendorTypes_by_VendorsVendorTypes;
// Map the vendorTypeIds to record.vendorTypes to get the array ED wants
record.vendorTypes = vendorTypeIds.map(function (item) {
return item.id;
});
// reset the record
payload.record = record;
// Sideload the vendorTypes array to the payload
payload.vendorTypes = vendorTypes;
// BOOM
return payload;
}
var payload = {
record: [
{
id: 2,
name: "3M Aerospace",
website: "http://solutions.3m.com/wps/portal/3M/en_US/Aerospace/Aircraft/",
isActive: true,
isQAActive: true,
createdBy: "32",
createdDate: "2014-10-01T15:19:35",
modifiedBy: null,
modifiedDate: null,
VendorsVendorTypes_by_vendorId: [
{
id: 2,
vendorId: 2,
vendorTypeId: 1
},
{
id: 3,
vendorId: 2,
vendorTypeId: 2
},
{
id: 4,
vendorId: 2,
vendorTypeId: 3
}
],
VendorTypes_by_VendorsVendorTypes: [
{
id: 1,
type: "Component Repair",
isActive: true,
createdBy: "32",
createdDate: "2014-09-30T14:05:34",
modifiedBy: "32",
modifiedDate: "2014-09-30T14:15:39"
},
{
id: 2,
type: "Parts Supplier",
isActive: true,
createdBy: "32",
createdDate: "2014-09-30T14:34:21",
modifiedBy: null,
modifiedDate: null
},
{
id: 3,
type: "Materials Supplier",
isActive: true,
createdBy: "32",
createdDate: "2014-09-30T14:35:21",
modifiedBy: "32",
modifiedDate: "2014-09-30T14:35:48"
}
]
}
]
}
var original = JSON.stringify(payload, undefined, 4);
var transformed = JSON.stringify(transform(payload), undefined, 4);
output(syntaxHighlight(original));
output(syntaxHighlight(transformed));
</script></body>
</html>
pre {outline: 1px solid #ccc; padding: 5px; margin: 5px; }
.string { color: green; }
.number { color: darkorange; }
.boolean { color: blue; }
.null { color: magenta; }
.key { color: red; }
function output(inp) {
document.body.appendChild(document.createElement('pre')).innerHTML = inp;
}
// pretty-print the JSON
function syntaxHighlight(json) {
json = json.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
return json.replace(/("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)/g, function (match) {
var cls = 'number';
if (/^"/.test(match)) {
if (/:$/.test(match)) {
cls = 'key';
} else {
cls = 'string';
}
} else if (/true|false/.test(match)) {
cls = 'boolean';
} else if (/null/.test(match)) {
cls = 'null';
}
return '<span class="' + cls + '">' + match + '</span>';
});
}
function transform(payload) {
// get the record
var record = payload.record[0];
// get the vendorTypeIds into their own var and remove from the original record
var vendorTypeIds = record.VendorsVendorTypes_by_vendorId;
delete record.VendorsVendorTypes_by_vendorId;
// get the vendorTypes into their own var and remove from the original record
var vendorTypes = record.VendorTypes_by_VendorsVendorTypes;
delete record.VendorTypes_by_VendorsVendorTypes;
// Map the vendorTypeIds to record.vendorTypes to get the array ED wants
record.vendorTypes = vendorTypeIds.map(function (item) {
return item.id;
});
// reset the record
payload.record = record;
// Sideload the vendorTypes array to the payload
payload.vendorTypes = vendorTypes;
// BOOM
return payload;
}
var payload = {
record: [
{
id: 2,
name: "3M Aerospace",
website: "http://solutions.3m.com/wps/portal/3M/en_US/Aerospace/Aircraft/",
isActive: true,
isQAActive: true,
createdBy: "32",
createdDate: "2014-10-01T15:19:35",
modifiedBy: null,
modifiedDate: null,
VendorsVendorTypes_by_vendorId: [
{
id: 2,
vendorId: 2,
vendorTypeId: 1
},
{
id: 3,
vendorId: 2,
vendorTypeId: 2
},
{
id: 4,
vendorId: 2,
vendorTypeId: 3
}
],
VendorTypes_by_VendorsVendorTypes: [
{
id: 1,
type: "Component Repair",
isActive: true,
createdBy: "32",
createdDate: "2014-09-30T14:05:34",
modifiedBy: "32",
modifiedDate: "2014-09-30T14:15:39"
},
{
id: 2,
type: "Parts Supplier",
isActive: true,
createdBy: "32",
createdDate: "2014-09-30T14:34:21",
modifiedBy: null,
modifiedDate: null
},
{
id: 3,
type: "Materials Supplier",
isActive: true,
createdBy: "32",
createdDate: "2014-09-30T14:35:21",
modifiedBy: "32",
modifiedDate: "2014-09-30T14:35:48"
}
]
}
]
}
var original = JSON.stringify(payload, undefined, 4);
var transformed = JSON.stringify(transform(payload), undefined, 4);
output(syntaxHighlight(original));
output(syntaxHighlight(transformed));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment