Skip to content

Instantly share code, notes, and snippets.

@skipjac
Last active April 20, 2016 20:28
Show Gist options
  • Save skipjac/6ea3ad0f179c2a71dd6d5850038c617f to your computer and use it in GitHub Desktop.
Save skipjac/6ea3ad0f179c2a71dd6d5850038c617f to your computer and use it in GitHub Desktop.
For order data
var data = 'Orders[Count=1, Order [customer=Customer [id=6a665314-44ac-4fd9-af73-ed975fb19018, name=Tarik, givenname=Taaza, birthday=Wed Apr 20 05:12:19 CEST 2016, plz=50933, street=Aachener Str., house=746-750, city=Köln], name=Installation, note=not ready, created=Wed Apr 20 05:12:19 CEST 2016, status=not ready]]'
function ordersJSONize(str) {
return str
//right bracket replacement
.replace(/]/g,'}')
//left bracket replacement
.replace(/\[/g,'{')
// equal sign replacement
.replace(/\b=|!(\s=)/g,':')
// wrap keys without quote with valid double quote
.replace(/([\$a-zA-z]+)\s*:/g, function(_, $1){return '"'+$1+'":'})
// quote data
.replace(/:([^\{,]*)\,/g, function(_, $1){return ':"'+$1+'",'})
// fix status quotes
.replace(/("status"):([^\{,]+?)\}/g, function(_, $1, $2){return ''+$1+':"'+$2+'"}'})
// fix city quotes
.replace(/("city"):([^\{,]*)\}/g, function(_, $1, $2){return ''+$1+':"'+$2+'"}'})
//fix double quotes
.replace(/""/g, '"')
// place : after Orders
.replace(/(Order.)/g, function(_, $1){return '"'+$1+'":'})
//remove Customer
.replace(/Customer/g, '')
.replace(/\}"/g, '}')
}
function docsJSONize(str) {
return str
//right bracket replacement
.replace(/]/g,'}')
//left bracket replacement
.replace(/\[/g,'{')
// equal sign replacement
.replace(/\b=(?!=)|!(\s=)/g,':')
//fix data
.replace(/data\(Base64\)/g, 'data:')
// wrap keys without quote with valid double quote
.replace(/([\$a-zA-z]+)\s*:/g, function(_, $1){return '"'+$1+'":'})
// quote data
.replace(/:([^\{,]*)\,/g, function(_, $1){return ':"'+$1+'",'})
// fix data quotes
.replace(/("data"):([^\{,]+?)\}/g, function(_, $1, $2){return ''+$1+':"'+$2+'"}'})
// fix city quotes
.replace(/("city"):([^\{,]*)\}/g, function(_, $1, $2){return ''+$1+':"'+$2+'"}'})
//fix double quotes
.replace(/""/g, '"')
// place : after Docs
.replace(/(Docs)/g, function(_, $1){return '"'+$1+'":'})
// place : after Document
.replace(/(Document)/g, function(_, $1){return '"'+$1+'":'})
//remove Customer
.replace(/Customer/g, '')
.replace(/\}"/g, '}')
}
function custJSONize(str) {
return str
//right bracket replacement
.replace(/]/g,'}')
//left bracket replacement
.replace(/\[/g,'{')
// equal sign replacement
.replace(/\b=|!(\s=)/g,':')
// wrap keys without quote with valid double quote
.replace(/([\$a-zA-z]+)\s*:/g, function(_, $1){return '"'+$1+'":'})
// quote data
.replace(/:([^\{,]*)\,/g, function(_, $1){return ':"'+$1+'",'})
// fix status quotes
.replace(/("status"):([^\{,]+?)\}/g, function(_, $1, $2){return ''+$1+':"'+$2+'"}'})
// fix city quotes
.replace(/("city"):([^\{,]*)\}/g, function(_, $1, $2){return ''+$1+':"'+$2+'"}'})
//fix double quotes
.replace(/""/g, '"')
.replace(/\}"/g, '}')
// fix root Customer
.replace(/(Customer)/g, function(_, $1){return '"'+$1+'":'})
}
@skipjac
Copy link
Author

skipjac commented Apr 20, 2016

run function

var json = JSONize(data);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment