2

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?

4
  • Why not capitalizing after parsing? Commented Nov 21, 2018 at 7:45
  • 1
    You only capitalized the first letter of the Key... presuming that by field you mean Key. Commented Nov 21, 2018 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. Commented Nov 21, 2018 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. Commented Nov 21, 2018 at 7:52

2 Answers 2

5

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" && !Array.isArray(value)) {
    // It's a non-null, non-array 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);

Sign up to request clarification or add additional context in comments.

5 Comments

I think you mean *as they're received?
Oh maybe you did mean revived lulz
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 - 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).
Thanks for this, this works great except for arrays. When value is an array it should be returned unmodified.
2

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

4 Comments

Object.keys(JSON.parse(v)) instead of Object.keys(v)?
Yeah sorry, I thought v was an object, I'll update my comment
"No, but..." Actually, you can, with a reviver.
@T.J.Crowder Wow, I didn't know that. Good to know!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.