0

I have this object, which we'll call JSONchunk:

{
    "stuffIWant": {
        "number": 123,
        "string": "string",
        "boolean": true
    },
    "boringDumbStuff": {
        "number": 456,
        "boolean": false
    }
}

I want to get every property of stuffIWant. However, I don't know all the properties that stuffIWant will contain at a given time.

I was hoping to use JSON.stringify(JSONchunk, ['stuffIWant']) but I only get part of the output I want. Namely, it returns {stuffIWant: {}}. Close, but no ciggie.

EDIT: Here's the result I'm looking for.

{
    "stuffIWant": {
        "number": 123,
        "string": "string",
        "boolean": true
    },
}

Is there a way to use a replacer array to return an object AND all of its properties without explicitly referencing those properties in the array? Or will I need to craft something a little more sophisticated?

5
  • please add an example of the wanted result. Commented Mar 27, 2016 at 20:31
  • To loop all properties of an object, use something like for(var i in obj.stuffIWant) {} Commented Mar 27, 2016 at 20:35
  • Added! I can't overstate that the properties listed there are not fixed, and may have more or less entries with completely different key names at any given time. Commented Mar 27, 2016 at 20:35
  • 2
    JSON.stringify(JSONchunk.stuffIWant)? Am I missing something? Commented Mar 27, 2016 at 20:38
  • 1
    I thought that you were looking something more complicated than just stringifying JSONchunk.stuffIWant. The question was artful ... thats funny Commented Mar 27, 2016 at 20:45

1 Answer 1

3

Simple:

JSON.stringify(JSONchunk.stuffIWant)
Sign up to request clarification or add additional context in comments.

2 Comments

Welp, I made that WAY harder than it needed to be. Thanks for this, it's much easier!
@Rydash Don't feel too bad. I'm sure Brendan Eich said the same thing after creating Javascript

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.