Renaming JSON field using JSON.parse
Say we have this JSON string:
const v = `{"foo":"bar"}`;
is there a way to configure parsing with JSON.parse so that a field gets renamed, for example capitalizing the field name:
const parsed = JSON.parse(v, captitalize);
console.log(parsed); // => {Foo: "bar"}
or some way to transform the field names, depending on which field you are working with?
javascript node.js json
add a comment |
Say we have this JSON string:
const v = `{"foo":"bar"}`;
is there a way to configure parsing with JSON.parse so that a field gets renamed, for example capitalizing the field name:
const parsed = JSON.parse(v, captitalize);
console.log(parsed); // => {Foo: "bar"}
or some way to transform the field names, depending on which field you are working with?
javascript node.js json
Why not capitalizing after parsing?
– vibhor1997a
Nov 21 '18 at 7:45
1
You only capitalized the first letter of the Key... presuming that by field you mean Key.
– lusitanica
Nov 21 '18 at 7:48
@vibhor1997a well using JSON libs in other languages, you can choose which source fields get parsed into which destination fields, there's more control. Capitalization was just an example, but I am looking for more configurability to turn the string into an object.
– Alexander Mills
Nov 21 '18 at 7:48
1
From MDN "If a reviver is specified, the value computed by parsing is transformed before being returned. " - So that's what you were referring to I presume, but this only effects the value and not the key, and You're asking about the key.
– Neil Lunn
Nov 21 '18 at 7:52
add a comment |
Say we have this JSON string:
const v = `{"foo":"bar"}`;
is there a way to configure parsing with JSON.parse so that a field gets renamed, for example capitalizing the field name:
const parsed = JSON.parse(v, captitalize);
console.log(parsed); // => {Foo: "bar"}
or some way to transform the field names, depending on which field you are working with?
javascript node.js json
Say we have this JSON string:
const v = `{"foo":"bar"}`;
is there a way to configure parsing with JSON.parse so that a field gets renamed, for example capitalizing the field name:
const parsed = JSON.parse(v, captitalize);
console.log(parsed); // => {Foo: "bar"}
or some way to transform the field names, depending on which field you are working with?
javascript node.js json
javascript node.js json
asked Nov 21 '18 at 7:43
Alexander MillsAlexander Mills
19.5k33158339
19.5k33158339
Why not capitalizing after parsing?
– vibhor1997a
Nov 21 '18 at 7:45
1
You only capitalized the first letter of the Key... presuming that by field you mean Key.
– lusitanica
Nov 21 '18 at 7:48
@vibhor1997a well using JSON libs in other languages, you can choose which source fields get parsed into which destination fields, there's more control. Capitalization was just an example, but I am looking for more configurability to turn the string into an object.
– Alexander Mills
Nov 21 '18 at 7:48
1
From MDN "If a reviver is specified, the value computed by parsing is transformed before being returned. " - So that's what you were referring to I presume, but this only effects the value and not the key, and You're asking about the key.
– Neil Lunn
Nov 21 '18 at 7:52
add a comment |
Why not capitalizing after parsing?
– vibhor1997a
Nov 21 '18 at 7:45
1
You only capitalized the first letter of the Key... presuming that by field you mean Key.
– lusitanica
Nov 21 '18 at 7:48
@vibhor1997a well using JSON libs in other languages, you can choose which source fields get parsed into which destination fields, there's more control. Capitalization was just an example, but I am looking for more configurability to turn the string into an object.
– Alexander Mills
Nov 21 '18 at 7:48
1
From MDN "If a reviver is specified, the value computed by parsing is transformed before being returned. " - So that's what you were referring to I presume, but this only effects the value and not the key, and You're asking about the key.
– Neil Lunn
Nov 21 '18 at 7:52
Why not capitalizing after parsing?
– vibhor1997a
Nov 21 '18 at 7:45
Why not capitalizing after parsing?
– vibhor1997a
Nov 21 '18 at 7:45
1
1
You only capitalized the first letter of the Key... presuming that by field you mean Key.
– lusitanica
Nov 21 '18 at 7:48
You only capitalized the first letter of the Key... presuming that by field you mean Key.
– lusitanica
Nov 21 '18 at 7:48
@vibhor1997a well using JSON libs in other languages, you can choose which source fields get parsed into which destination fields, there's more control. Capitalization was just an example, but I am looking for more configurability to turn the string into an object.
– Alexander Mills
Nov 21 '18 at 7:48
@vibhor1997a well using JSON libs in other languages, you can choose which source fields get parsed into which destination fields, there's more control. Capitalization was just an example, but I am looking for more configurability to turn the string into an object.
– Alexander Mills
Nov 21 '18 at 7:48
1
1
From MDN "If a reviver is specified, the value computed by parsing is transformed before being returned. " - So that's what you were referring to I presume, but this only effects the value and not the key, and You're asking about the key.
– Neil Lunn
Nov 21 '18 at 7:52
From MDN "If a reviver is specified, the value computed by parsing is transformed before being returned. " - So that's what you were referring to I presume, but this only effects the value and not the key, and You're asking about the key.
– Neil Lunn
Nov 21 '18 at 7:52
add a comment |
2 Answers
2
active
oldest
votes
You can use the reviver parameter to JSON.parse
to modify objects as they're revived:
const v = `{"foo":"bar"}`;
const result = JSON.parse(v, (name, value) => {
if (value && typeof value === "object") {
// It's a non-null object, create a replacement with the keys initially-capped
const newValue = {};
for (const key in value) {
newValue[key.charAt(0).toUpperCase() + key.slice(1)] = value[key];
}
return newValue;
}
return value;
});
console.log(result);
I think you mean *as they're received?
– Alexander Mills
Nov 21 '18 at 8:03
Oh maybe you did mean revived lulz
– Alexander Mills
Nov 21 '18 at 8:03
Can you please add more about how this works and how reviver works in general? By reading MDN docs for reviver I thought it wasn't possible too change the key.
– vibhor1997a
Nov 21 '18 at 8:04
@vibhor1997a - Did you review the code and the comments in it? You can't (sadly) change the key before hte object is created, but you can replace that object afterward with updated keys (which is what the above does).
– T.J. Crowder
Nov 21 '18 at 12:17
add a comment |
You can do the following:
// Better use try-catch here
const parsedV = JSON.parse(v);
const parsed = Object.keys(parsedV).reduce((acc, key) => {
acc[capitalize(key)] = parsedV[key];
return acc;
}, {});
Object.keys(JSON.parse(v))
instead ofObject.keys(v)
?
– vibhor1997a
Nov 21 '18 at 7:49
Yeah sorry, I thoughtv
was an object, I'll update my comment
– Gilad Bar
Nov 21 '18 at 7:50
1
"No, but..." Actually, you can, with a reviver.
– T.J. Crowder
Nov 21 '18 at 7:59
@T.J.Crowder Wow, I didn't know that. Good to know!
– Gilad Bar
Nov 21 '18 at 8:03
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%2f53407337%2frenaming-json-field-using-json-parse%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can use the reviver parameter to JSON.parse
to modify objects as they're revived:
const v = `{"foo":"bar"}`;
const result = JSON.parse(v, (name, value) => {
if (value && typeof value === "object") {
// It's a non-null object, create a replacement with the keys initially-capped
const newValue = {};
for (const key in value) {
newValue[key.charAt(0).toUpperCase() + key.slice(1)] = value[key];
}
return newValue;
}
return value;
});
console.log(result);
I think you mean *as they're received?
– Alexander Mills
Nov 21 '18 at 8:03
Oh maybe you did mean revived lulz
– Alexander Mills
Nov 21 '18 at 8:03
Can you please add more about how this works and how reviver works in general? By reading MDN docs for reviver I thought it wasn't possible too change the key.
– vibhor1997a
Nov 21 '18 at 8:04
@vibhor1997a - Did you review the code and the comments in it? You can't (sadly) change the key before hte object is created, but you can replace that object afterward with updated keys (which is what the above does).
– T.J. Crowder
Nov 21 '18 at 12:17
add a comment |
You can use the reviver parameter to JSON.parse
to modify objects as they're revived:
const v = `{"foo":"bar"}`;
const result = JSON.parse(v, (name, value) => {
if (value && typeof value === "object") {
// It's a non-null object, create a replacement with the keys initially-capped
const newValue = {};
for (const key in value) {
newValue[key.charAt(0).toUpperCase() + key.slice(1)] = value[key];
}
return newValue;
}
return value;
});
console.log(result);
I think you mean *as they're received?
– Alexander Mills
Nov 21 '18 at 8:03
Oh maybe you did mean revived lulz
– Alexander Mills
Nov 21 '18 at 8:03
Can you please add more about how this works and how reviver works in general? By reading MDN docs for reviver I thought it wasn't possible too change the key.
– vibhor1997a
Nov 21 '18 at 8:04
@vibhor1997a - Did you review the code and the comments in it? You can't (sadly) change the key before hte object is created, but you can replace that object afterward with updated keys (which is what the above does).
– T.J. Crowder
Nov 21 '18 at 12:17
add a comment |
You can use the reviver parameter to JSON.parse
to modify objects as they're revived:
const v = `{"foo":"bar"}`;
const result = JSON.parse(v, (name, value) => {
if (value && typeof value === "object") {
// It's a non-null object, create a replacement with the keys initially-capped
const newValue = {};
for (const key in value) {
newValue[key.charAt(0).toUpperCase() + key.slice(1)] = value[key];
}
return newValue;
}
return value;
});
console.log(result);
You can use the reviver parameter to JSON.parse
to modify objects as they're revived:
const v = `{"foo":"bar"}`;
const result = JSON.parse(v, (name, value) => {
if (value && typeof value === "object") {
// It's a non-null object, create a replacement with the keys initially-capped
const newValue = {};
for (const key in value) {
newValue[key.charAt(0).toUpperCase() + key.slice(1)] = value[key];
}
return newValue;
}
return value;
});
console.log(result);
const v = `{"foo":"bar"}`;
const result = JSON.parse(v, (name, value) => {
if (value && typeof value === "object") {
// It's a non-null object, create a replacement with the keys initially-capped
const newValue = {};
for (const key in value) {
newValue[key.charAt(0).toUpperCase() + key.slice(1)] = value[key];
}
return newValue;
}
return value;
});
console.log(result);
const v = `{"foo":"bar"}`;
const result = JSON.parse(v, (name, value) => {
if (value && typeof value === "object") {
// It's a non-null object, create a replacement with the keys initially-capped
const newValue = {};
for (const key in value) {
newValue[key.charAt(0).toUpperCase() + key.slice(1)] = value[key];
}
return newValue;
}
return value;
});
console.log(result);
answered Nov 21 '18 at 7:57
T.J. CrowderT.J. Crowder
692k12212311326
692k12212311326
I think you mean *as they're received?
– Alexander Mills
Nov 21 '18 at 8:03
Oh maybe you did mean revived lulz
– Alexander Mills
Nov 21 '18 at 8:03
Can you please add more about how this works and how reviver works in general? By reading MDN docs for reviver I thought it wasn't possible too change the key.
– vibhor1997a
Nov 21 '18 at 8:04
@vibhor1997a - Did you review the code and the comments in it? You can't (sadly) change the key before hte object is created, but you can replace that object afterward with updated keys (which is what the above does).
– T.J. Crowder
Nov 21 '18 at 12:17
add a comment |
I think you mean *as they're received?
– Alexander Mills
Nov 21 '18 at 8:03
Oh maybe you did mean revived lulz
– Alexander Mills
Nov 21 '18 at 8:03
Can you please add more about how this works and how reviver works in general? By reading MDN docs for reviver I thought it wasn't possible too change the key.
– vibhor1997a
Nov 21 '18 at 8:04
@vibhor1997a - Did you review the code and the comments in it? You can't (sadly) change the key before hte object is created, but you can replace that object afterward with updated keys (which is what the above does).
– T.J. Crowder
Nov 21 '18 at 12:17
I think you mean *as they're received?
– Alexander Mills
Nov 21 '18 at 8:03
I think you mean *as they're received?
– Alexander Mills
Nov 21 '18 at 8:03
Oh maybe you did mean revived lulz
– Alexander Mills
Nov 21 '18 at 8:03
Oh maybe you did mean revived lulz
– Alexander Mills
Nov 21 '18 at 8:03
Can you please add more about how this works and how reviver works in general? By reading MDN docs for reviver I thought it wasn't possible too change the key.
– vibhor1997a
Nov 21 '18 at 8:04
Can you please add more about how this works and how reviver works in general? By reading MDN docs for reviver I thought it wasn't possible too change the key.
– vibhor1997a
Nov 21 '18 at 8:04
@vibhor1997a - Did you review the code and the comments in it? You can't (sadly) change the key before hte object is created, but you can replace that object afterward with updated keys (which is what the above does).
– T.J. Crowder
Nov 21 '18 at 12:17
@vibhor1997a - Did you review the code and the comments in it? You can't (sadly) change the key before hte object is created, but you can replace that object afterward with updated keys (which is what the above does).
– T.J. Crowder
Nov 21 '18 at 12:17
add a comment |
You can do the following:
// Better use try-catch here
const parsedV = JSON.parse(v);
const parsed = Object.keys(parsedV).reduce((acc, key) => {
acc[capitalize(key)] = parsedV[key];
return acc;
}, {});
Object.keys(JSON.parse(v))
instead ofObject.keys(v)
?
– vibhor1997a
Nov 21 '18 at 7:49
Yeah sorry, I thoughtv
was an object, I'll update my comment
– Gilad Bar
Nov 21 '18 at 7:50
1
"No, but..." Actually, you can, with a reviver.
– T.J. Crowder
Nov 21 '18 at 7:59
@T.J.Crowder Wow, I didn't know that. Good to know!
– Gilad Bar
Nov 21 '18 at 8:03
add a comment |
You can do the following:
// Better use try-catch here
const parsedV = JSON.parse(v);
const parsed = Object.keys(parsedV).reduce((acc, key) => {
acc[capitalize(key)] = parsedV[key];
return acc;
}, {});
Object.keys(JSON.parse(v))
instead ofObject.keys(v)
?
– vibhor1997a
Nov 21 '18 at 7:49
Yeah sorry, I thoughtv
was an object, I'll update my comment
– Gilad Bar
Nov 21 '18 at 7:50
1
"No, but..." Actually, you can, with a reviver.
– T.J. Crowder
Nov 21 '18 at 7:59
@T.J.Crowder Wow, I didn't know that. Good to know!
– Gilad Bar
Nov 21 '18 at 8:03
add a comment |
You can do the following:
// Better use try-catch here
const parsedV = JSON.parse(v);
const parsed = Object.keys(parsedV).reduce((acc, key) => {
acc[capitalize(key)] = parsedV[key];
return acc;
}, {});
You can do the following:
// Better use try-catch here
const parsedV = JSON.parse(v);
const parsed = Object.keys(parsedV).reduce((acc, key) => {
acc[capitalize(key)] = parsedV[key];
return acc;
}, {});
edited Nov 21 '18 at 8:09
answered Nov 21 '18 at 7:46
Gilad BarGilad Bar
753314
753314
Object.keys(JSON.parse(v))
instead ofObject.keys(v)
?
– vibhor1997a
Nov 21 '18 at 7:49
Yeah sorry, I thoughtv
was an object, I'll update my comment
– Gilad Bar
Nov 21 '18 at 7:50
1
"No, but..." Actually, you can, with a reviver.
– T.J. Crowder
Nov 21 '18 at 7:59
@T.J.Crowder Wow, I didn't know that. Good to know!
– Gilad Bar
Nov 21 '18 at 8:03
add a comment |
Object.keys(JSON.parse(v))
instead ofObject.keys(v)
?
– vibhor1997a
Nov 21 '18 at 7:49
Yeah sorry, I thoughtv
was an object, I'll update my comment
– Gilad Bar
Nov 21 '18 at 7:50
1
"No, but..." Actually, you can, with a reviver.
– T.J. Crowder
Nov 21 '18 at 7:59
@T.J.Crowder Wow, I didn't know that. Good to know!
– Gilad Bar
Nov 21 '18 at 8:03
Object.keys(JSON.parse(v))
instead of Object.keys(v)
?– vibhor1997a
Nov 21 '18 at 7:49
Object.keys(JSON.parse(v))
instead of Object.keys(v)
?– vibhor1997a
Nov 21 '18 at 7:49
Yeah sorry, I thought
v
was an object, I'll update my comment– Gilad Bar
Nov 21 '18 at 7:50
Yeah sorry, I thought
v
was an object, I'll update my comment– Gilad Bar
Nov 21 '18 at 7:50
1
1
"No, but..." Actually, you can, with a reviver.
– T.J. Crowder
Nov 21 '18 at 7:59
"No, but..." Actually, you can, with a reviver.
– T.J. Crowder
Nov 21 '18 at 7:59
@T.J.Crowder Wow, I didn't know that. Good to know!
– Gilad Bar
Nov 21 '18 at 8:03
@T.J.Crowder Wow, I didn't know that. Good to know!
– Gilad Bar
Nov 21 '18 at 8:03
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%2f53407337%2frenaming-json-field-using-json-parse%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
Why not capitalizing after parsing?
– vibhor1997a
Nov 21 '18 at 7:45
1
You only capitalized the first letter of the Key... presuming that by field you mean Key.
– lusitanica
Nov 21 '18 at 7:48
@vibhor1997a well using JSON libs in other languages, you can choose which source fields get parsed into which destination fields, there's more control. Capitalization was just an example, but I am looking for more configurability to turn the string into an object.
– Alexander Mills
Nov 21 '18 at 7:48
1
From MDN "If a reviver is specified, the value computed by parsing is transformed before being returned. " - So that's what you were referring to I presume, but this only effects the value and not the key, and You're asking about the key.
– Neil Lunn
Nov 21 '18 at 7:52