0

I want one json object but get multiple.

let example = {
  test1: '',
  test2: '',
  oTest: {
    oTest1: '',
    oTest2: ''
  },
  ooTest: {
    ooTest1: '',
    ooTest2: null,
  }
};

let result = JSON.stringify({
  example,
  'oTest': example.oTest,
  'ooTest': example.ooTest
});

console.log(JSON.stringify(example));

My Result is that i get 3 seperate Json Objects(example,oTest,ooTest) but my goal is to have 1 Json Object "example" including oTest and ooTest. Also "example" dont show oTest and ooTest.

Goal:

{
  "test1": "",
  "test2": "",
  "oTest": {
    "oTest1": "",
    "oTest2": ""
  },
  "ooTest": {
    "ooTest1": "",
    "ooTest2": ""
  }
}
4
  • 1
    What output are you expecting exactly? {test1:'', test2:'', oTest:''...} or {example: {test1:'' ...}} or what exactly? Commented May 14, 2019 at 9:37
  • 1
    Your code is doing what JSON.stringify(example) would already do for you. What are you trying to achieve here? Commented May 14, 2019 at 9:48
  • Please show the exact expected JSON you want as result. Commented May 14, 2019 at 9:50
  • is your goal ?? { "test1": "", "test2": "", "oTest1": "", "oTest2": "", "ooTest1": "", "ooTest2": null } coz it seems your expected result is same as your original object Commented May 14, 2019 at 10:00

2 Answers 2

3

What you are doing doesn't make sense at all. Your expected result is returned by just passing example to JSON.stringify

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

1 Comment

This dont work because it ignores oTest and ooTest and only shows "test1" and "test2"
-1
JSON.stringify(Object.assign(example,{'oTest': example.oTest},{'ooTest': example.ooTest})

This is merge all the 3 object in one, and return back a stringified object.

3 Comments

Explain what your answer does.
This one did help thanks :) Sorry for confusing im new to programming
This does exactly the same that JSON.stringify(example) does.

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.