Renaming JSON field using JSON.parse












1















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?










share|improve this question























  • 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


















1















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?










share|improve this question























  • 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
















1












1








1








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?










share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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





















  • 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














2 Answers
2






active

oldest

votes


















4














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);








share|improve this answer
























  • 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



















1














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;
}, {});





share|improve this answer


























  • 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






  • 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











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
});


}
});














draft saved

draft discarded


















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









4














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);








share|improve this answer
























  • 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
















4














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);








share|improve this answer
























  • 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














4












4








4







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);








share|improve this answer













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);






share|improve this answer












share|improve this answer



share|improve this answer










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



















  • 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













1














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;
}, {});





share|improve this answer


























  • 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






  • 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
















1














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;
}, {});





share|improve this answer


























  • 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






  • 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














1












1








1







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;
}, {});





share|improve this answer















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;
}, {});






share|improve this answer














share|improve this answer



share|improve this answer








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 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






  • 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











  • Yeah sorry, I thought v 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


















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

mysqli_query(): Empty query in /home/lucindabrummitt/public_html/blog/wp-includes/wp-db.php on line 1924

How to change which sound is reproduced for terminal bell?

Can I use Tabulator js library in my java Spring + Thymeleaf project?