Adding quotations to stringified JSON object values
My stringified object data doesn't include quotation marks around object values, which errors when attempting JSON.parse()
:
'{ "affiliation": CORPORATE, "userId": 75c35d1c-5d12-4485-8fa8-b2f1551a3e6e }'
I need the string to be:
'{ "affiliation": "CORPORATE", "userId": "75c35d1c-5d12-4485-8fa8-b2f1551a3e6e" }'
I'm using this regex to add quotes to the object keys:
var newStr = str.replace(/(['"])?([a-z0-9A-Z_]+)(['"])?:/g, '"$2": ');
For instance:
'{ affiliation: CORPORATE }'
to '{ "affiliation": CORPORATE }'
There are only string values in my data, so I don't need to check value types. How can I alter my regex expression to add quotations to object values as well?
javascript regex replace
add a comment |
My stringified object data doesn't include quotation marks around object values, which errors when attempting JSON.parse()
:
'{ "affiliation": CORPORATE, "userId": 75c35d1c-5d12-4485-8fa8-b2f1551a3e6e }'
I need the string to be:
'{ "affiliation": "CORPORATE", "userId": "75c35d1c-5d12-4485-8fa8-b2f1551a3e6e" }'
I'm using this regex to add quotes to the object keys:
var newStr = str.replace(/(['"])?([a-z0-9A-Z_]+)(['"])?:/g, '"$2": ');
For instance:
'{ affiliation: CORPORATE }'
to '{ "affiliation": CORPORATE }'
There are only string values in my data, so I don't need to check value types. How can I alter my regex expression to add quotations to object values as well?
javascript regex replace
@ACD my regex currently adds quotes to the object key, but doesn't add quotes to the object value
– alyx
Nov 16 '18 at 4:01
Is it really impossible to fix the other side that generates it incorrectly?
– zerkms
Nov 16 '18 at 4:03
@zerkms Yes, this data is coming from a neo4j dump converted to CVS but the original source doesn't wrap quotes
– alyx
Nov 16 '18 at 4:04
is there a guarantee that special values like spaces,,
:
or"
will not be present in the stringified object? If not, how would you add quotes to{ a key: a value, maybe key: value: 2}
?
– lucascaro
Nov 16 '18 at 4:24
add a comment |
My stringified object data doesn't include quotation marks around object values, which errors when attempting JSON.parse()
:
'{ "affiliation": CORPORATE, "userId": 75c35d1c-5d12-4485-8fa8-b2f1551a3e6e }'
I need the string to be:
'{ "affiliation": "CORPORATE", "userId": "75c35d1c-5d12-4485-8fa8-b2f1551a3e6e" }'
I'm using this regex to add quotes to the object keys:
var newStr = str.replace(/(['"])?([a-z0-9A-Z_]+)(['"])?:/g, '"$2": ');
For instance:
'{ affiliation: CORPORATE }'
to '{ "affiliation": CORPORATE }'
There are only string values in my data, so I don't need to check value types. How can I alter my regex expression to add quotations to object values as well?
javascript regex replace
My stringified object data doesn't include quotation marks around object values, which errors when attempting JSON.parse()
:
'{ "affiliation": CORPORATE, "userId": 75c35d1c-5d12-4485-8fa8-b2f1551a3e6e }'
I need the string to be:
'{ "affiliation": "CORPORATE", "userId": "75c35d1c-5d12-4485-8fa8-b2f1551a3e6e" }'
I'm using this regex to add quotes to the object keys:
var newStr = str.replace(/(['"])?([a-z0-9A-Z_]+)(['"])?:/g, '"$2": ');
For instance:
'{ affiliation: CORPORATE }'
to '{ "affiliation": CORPORATE }'
There are only string values in my data, so I don't need to check value types. How can I alter my regex expression to add quotations to object values as well?
javascript regex replace
javascript regex replace
edited Nov 16 '18 at 4:00
alyx
asked Nov 16 '18 at 3:55
alyxalyx
1,14662549
1,14662549
@ACD my regex currently adds quotes to the object key, but doesn't add quotes to the object value
– alyx
Nov 16 '18 at 4:01
Is it really impossible to fix the other side that generates it incorrectly?
– zerkms
Nov 16 '18 at 4:03
@zerkms Yes, this data is coming from a neo4j dump converted to CVS but the original source doesn't wrap quotes
– alyx
Nov 16 '18 at 4:04
is there a guarantee that special values like spaces,,
:
or"
will not be present in the stringified object? If not, how would you add quotes to{ a key: a value, maybe key: value: 2}
?
– lucascaro
Nov 16 '18 at 4:24
add a comment |
@ACD my regex currently adds quotes to the object key, but doesn't add quotes to the object value
– alyx
Nov 16 '18 at 4:01
Is it really impossible to fix the other side that generates it incorrectly?
– zerkms
Nov 16 '18 at 4:03
@zerkms Yes, this data is coming from a neo4j dump converted to CVS but the original source doesn't wrap quotes
– alyx
Nov 16 '18 at 4:04
is there a guarantee that special values like spaces,,
:
or"
will not be present in the stringified object? If not, how would you add quotes to{ a key: a value, maybe key: value: 2}
?
– lucascaro
Nov 16 '18 at 4:24
@ACD my regex currently adds quotes to the object key, but doesn't add quotes to the object value
– alyx
Nov 16 '18 at 4:01
@ACD my regex currently adds quotes to the object key, but doesn't add quotes to the object value
– alyx
Nov 16 '18 at 4:01
Is it really impossible to fix the other side that generates it incorrectly?
– zerkms
Nov 16 '18 at 4:03
Is it really impossible to fix the other side that generates it incorrectly?
– zerkms
Nov 16 '18 at 4:03
@zerkms Yes, this data is coming from a neo4j dump converted to CVS but the original source doesn't wrap quotes
– alyx
Nov 16 '18 at 4:04
@zerkms Yes, this data is coming from a neo4j dump converted to CVS but the original source doesn't wrap quotes
– alyx
Nov 16 '18 at 4:04
is there a guarantee that special values like spaces,
,
:
or "
will not be present in the stringified object? If not, how would you add quotes to { a key: a value, maybe key: value: 2}
?– lucascaro
Nov 16 '18 at 4:24
is there a guarantee that special values like spaces,
,
:
or "
will not be present in the stringified object? If not, how would you add quotes to { a key: a value, maybe key: value: 2}
?– lucascaro
Nov 16 '18 at 4:24
add a comment |
1 Answer
1
active
oldest
votes
You can use the following regex:
/[ ](?=b)|b(?=,|[ ])/g
It starts matching a Space and use a look ahead for a Word boundary
. It then uses an alternation for a Word boundary
followed by a comma
or a Space
.
It uses the 'global'
flag to match all.
Then you replace the matches with a double quote
.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53331208%2fadding-quotations-to-stringified-json-object-values%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can use the following regex:
/[ ](?=b)|b(?=,|[ ])/g
It starts matching a Space and use a look ahead for a Word boundary
. It then uses an alternation for a Word boundary
followed by a comma
or a Space
.
It uses the 'global'
flag to match all.
Then you replace the matches with a double quote
.
add a comment |
You can use the following regex:
/[ ](?=b)|b(?=,|[ ])/g
It starts matching a Space and use a look ahead for a Word boundary
. It then uses an alternation for a Word boundary
followed by a comma
or a Space
.
It uses the 'global'
flag to match all.
Then you replace the matches with a double quote
.
add a comment |
You can use the following regex:
/[ ](?=b)|b(?=,|[ ])/g
It starts matching a Space and use a look ahead for a Word boundary
. It then uses an alternation for a Word boundary
followed by a comma
or a Space
.
It uses the 'global'
flag to match all.
Then you replace the matches with a double quote
.
You can use the following regex:
/[ ](?=b)|b(?=,|[ ])/g
It starts matching a Space and use a look ahead for a Word boundary
. It then uses an alternation for a Word boundary
followed by a comma
or a Space
.
It uses the 'global'
flag to match all.
Then you replace the matches with a double quote
.
edited Nov 16 '18 at 4:40
answered Nov 16 '18 at 4:32
Poul BakPoul Bak
5,48831233
5,48831233
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53331208%2fadding-quotations-to-stringified-json-object-values%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
@ACD my regex currently adds quotes to the object key, but doesn't add quotes to the object value
– alyx
Nov 16 '18 at 4:01
Is it really impossible to fix the other side that generates it incorrectly?
– zerkms
Nov 16 '18 at 4:03
@zerkms Yes, this data is coming from a neo4j dump converted to CVS but the original source doesn't wrap quotes
– alyx
Nov 16 '18 at 4:04
is there a guarantee that special values like spaces,
,
:
or"
will not be present in the stringified object? If not, how would you add quotes to{ a key: a value, maybe key: value: 2}
?– lucascaro
Nov 16 '18 at 4:24