Using recursion to create a JSON string (replicate stringify)

Milos

I'm trying to replicate JSON.stringify() using recursion. I'm a bit confused as to why I'm getting undefined in my returned JSON string. Here's what I've done so far:

var testobj = {num:123,str:"This is a string",bool:true,o:{x:1,y:2},iamnull:null}
var count = 0
var stringifyJSON = function(obj) {

  // your code goes here
  if(obj === undefined){ 
  	return console.log("obj was undefined");
  }
  count = count + 1
  if(count > 20){  //I put this here mostly to stop infinite loops assuming this function wouldn't occur over 20 times.
  	return console.log("failed")
  }  
  var endarray = [];
if(obj !== undefined && typeof(obj)==='object'){  //If the object isn't undefined and the object is an object...

  for(var prop in obj){
   	console.log('"' + prop + '"');
  	endarray.push(stringifyJSON(prop) +'"' + ':' +'"'+ stringifyJSON(obj[prop])) //push the items into the array, using recursion. 
  }
  if(typeof(obj) !== undefined){
  	return '{' + endarray.join() + '}'
  }
}

  
  
};

//end result is "{undefined":"undefined,undefined":"undefined,undefined":"undefined,undefined":"{undefined":"undefined,undefined":"undefined},undefined":"{}}"

//JSON values cannot be a function, a date, or undefined

Could someone indicate where I'm going wrong? With recursion I'm having a problem identifying the source of the issue.

Donovan McMurray

There are a couple of things needed to get to the correct solution.

First, you don't have a base case for your recursion, so at the base level of each recursive trace, nothing is returned (i.e., undefined is implicitly returned). So first you must add a base case where ints, strings, booleans, and other primitive types, are converted to strings.

Second, you must also check that obj !== null before your recursive call, because typeof(null) evaluates to "object", strangely enough.

var testobj = {num:123,str:"This is a string",bool:true,o:{x:1,y:2},iamnull:null}
var count = 0
var stringifyJSON = function(obj) {

  // your code goes here
  if(obj === undefined){ 
    return console.log("obj was undefined");
  }
  count = count + 1
  if(count > 20){  //I put this here mostly to stop infinite loops assuming this function wouldn't occur over 20 times.
    return console.log("failed")
  }  
  var endarray = [];
if(obj !== undefined && obj !== null && typeof(obj)==='object'){  //If the object isn't undefined and the object is an object...

  for(var prop in obj){
    console.log('"' + prop + '"');
    endarray.push(stringifyJSON(prop) +'"' + ':' +'"'+ stringifyJSON(obj[prop])) //push the items into the array, using recursion. 
  }
  if(typeof(obj) !== undefined){
    return '{' + endarray.join() + '}'
  }
}
if (obj !== undefined) {return String(obj)}


};

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

scheme string-append? recursion to replicate a string

From Dev

Converting JS object to json string using JSON.stringify

From Dev

Converting JS object to json string using JSON.stringify

From Dev

convert object to string using JSON.stringify show empty object

From Java

Is it not possible to stringify an Error using JSON.stringify?

From Dev

JSON stringify returns empty string

From Dev

Java - using recursion to create all substrings from a string

From Dev

Create JSON String using GSON

From Dev

Evaluating a string using recursion

From Dev

Recursion using a String

From Dev

String permutations using recursion

From Dev

Reversing a String Using Recursion

From Dev

reverse string using recursion

From Dev

Using Recursion to Reverse a String

From Dev

Recursion using JSON

From Dev

Why do I get square brackets when trying to convert array to JSON string using stringify

From Dev

JSON.stringify turned the value array into a string

From Dev

Different output when using JSON.stringify

From Dev

Problem when using JSON.stringify in NodeJS

From Dev

Problem when using JSON.stringify in NodeJS

From Dev

Using stringify for nested JSON to use in localStorage

From Dev

Different output when using JSON.stringify

From Dev

How to Stringify the json in java without using js

From Dev

How to reverse a string using recursion?

From Dev

String permutations using recursion in Java

From Dev

Reversing a string in C using Recursion

From Dev

Using recursion to find a character in a string

From Dev

Using recursion to find a character in a string

From Dev

c: invert string using recursion?