0

I want to convert full object to String in JS.

The Object is as below :-

{ parent : { "a":1, testFunc(){ return 2;} } }

When i use JSON.stringify({parent : {"a":1, testFunc(){ return 2;} } })

-Actual :- I get "{"parent":{"a":1}}" as the output

- Expected :-

but i wanted output as "{"parent":{"a":1,testFunc(){return 2;}}}"

Is there any way to keep the function's in string as JSON.stringify removes the function.

Vishal Patil
  • 381
  • 3
  • 15
  • `JS Object != JSON` – User863 Aug 07 '19 at 11:37
  • This is expected. Functions are removed from the output string. Read this: https://stackoverflow.com/questions/7759200/is-there-any-possibility-to-have-json-stringify-preserve-functions – Sergiu Paraschiv Aug 07 '19 at 11:37
  • *"A value can be a string in double quotes, or a number, or true or false or null, or an object or an array."* https://www.json.org/ – adiga Aug 07 '19 at 11:57
  • you need to parse it in to JSONObject obj=new JSONObject(str); where you will get complete string in json object – Amitsharma Aug 07 '19 at 12:52

0 Answers0