0

Am writing a JS logic to form the below Json format structure which i need to pass as a parameter to a REST GET call.

{  
   "abc":{  
      "name":"abcname"
   },
   "def":{  
      "name":"defname",
      "type":"xyz"
   },
   "employees":{  
      "size":"4000000000"
   },
   "recommend":{  
      "range":"456"
   }
};

when i write the below logic it gives me an extra " character before and after {}

var abcObject = new Object();
abcObject.name = "abcname";
var abcjsonObject = new Object();
abcjsonObject.abc = JSON.stringify(abcObject);;
var myString = JSON.stringify(abcjsonObject);
console.log("myString" + myString);

1
  • can you show me your expected result? Commented Jul 20, 2017 at 4:59

2 Answers 2

1

Only stringify the top level object/value. Replace

abcjsonObject.abc = JSON.stringify(abcObject);

with

abcjsonObject.abc = abcObject;

FWIW, more streamed lined way of creating the object would be

var abcjsonObject = {abc: {name: 'abcname'}};
Sign up to request clarification or add additional context in comments.

Comments

0

Try this :

var jsonObj = new Object();
jsonObj.name = "abcname";
var abcjsonObj = new Object();
abcjsonObj.abc = jsonObj;
console.log(JSON.stringify(abcjsonObj));

Comments

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.