Skip to content

Converting object into Json (stringify) #1969

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
sbala69gmail opened this issue Dec 13, 2018 · 1 comment
Closed

Converting object into Json (stringify) #1969

sbala69gmail opened this issue Dec 13, 2018 · 1 comment

Comments

@sbala69gmail
Copy link

Hi ,

I have one doubt in stringify an object.

In input is below:

var obj = [{'name' : 'Jenisha', 'lastName' : 'dalin'}];
obj.__used = true;

My expected output:

JSON.stringify(obj, undefined, 4);
"[
    {
        "name": "Jenisha",
        "lastName": "dalin"
    }
],
"__used" : true
"

But the results are:

JSON.stringify(obj, undefined, 4);
"[
    {
        "name": "Jenisha",
        "lastName": "dalin"
    }
]"

"__used" param removed. is any alternate function available to stringify an object.

Thanks in advance.

@ljharb
Copy link
Collaborator

ljharb commented Dec 13, 2018

In this case, you’re attaching the property to the array, and while arrays are objects that can have properties, in JSON, the stringify algorithm ignores those.

If you have an object at the top level, then you can stick properties on that.

This repo is not for general javascript help, however - it’s for our javascript style guide - so I’m closing the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants