2

Here is a minified example of my problem:

var myObj = {hello: {bye: "bye"} };

JSON.stringify(myObj, ['hello']);

returns "{"hello": {}}"

How can I access nested objects using the replacer array parameter?

I researched posts demonstrating the "data" parameter in an ajax request, I am not making an ajax request, I am reading json, filtering properties, and writing the results to a new file.

2
  • 3
    The replacer can be a function too, you should be able do the logic you need. Commented Mar 9, 2015 at 23:01
  • @HelloWorld what exactly are you trying to do? If you're doing filtering of object values, it's probably better to use something like lodash/underscore for that. Commented Mar 10, 2015 at 15:35

1 Answer 1

3

The following seems to work:

JSON.stringify(myObj, ['hello', 'bye']);

But couldn't find any documentation on the topic.

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

2 Comments

this does not effect nested objects in my trials
I haven't found an easy way to do that... initial thought was to just override JSON.stringify, add a 'global' replacer that applies the keys to keep to every nested call, but that's not how JSON.stringify works, if a property is not to be included in the result, any keys that don't match the replacer array are simply not traversed, e.g. the replacer never gets invoked...if someone has a nice and concise solution for this "problem", I'd love 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.