Try to search your question here, if you can't find : Ask Any Question Now ?

Cannot get correct result using JSON.stringify() and JSON.parse()

HomeCategory: stackoverflowCannot get correct result using JSON.stringify() and JSON.parse()
Avatarkundan asked 5 months ago

Trying to retrieve the data from API and use them as objects.
The problem is I did not get the correct result after JSON.parse() and I do not know how to access the data (not knowing the index or parameters to retrieve specific data).

Function in Express using axios:

var result_post = axios.post(url_post, post_data, post_config);

result_post.then(function (res){
  //console.log("RESPONSE: ", res);
  json_result_post = res['data'];
  json_result_post = JSON.stringify(json_result_post, null, 2);
  json_result_post_parse = JSON.parse(json_result_post);
  console.log(json_result_post_parse);
  //cconsole.log(json_result_post);
  fs.writeFile("data_post.json", json_result_post, function(err){
    if(err){
      console.log(err);
    }
  });
  //json_result_post_parse = JSON.parse(json_result_post);

  //console.log("RESPONSE: ", res);
})
.catch(function (err){
  console.log("AXIOS ERROR: ", err);
});

Some Data after JSON.stringify():

{
  "body-json": {
    "emp_no": 80000,
    "email": "80000@cloud-spartan.com",
    "first_name": "Odoardo",
    "last_name": "Ranft",
    "birth_date": "1963-06-23",
    "gender": "F",
    "hire_date": "1994-07-03",
    "salaries": [
      {
        "salary": 40000,
        "from_date": "1994-07-03",
        "to_date": "1995-07-03"
      },
      {
        "salary": 41003,
        "from_date": "1995-07-03",
        "to_date": "1996-07-02"
      },
      {
        "salary": 41720,
        "from_date": "1996-07-02",
        "to_date": "1997-07-02"
      },
      {
        "salary": 44005,
        "from_date": "1997-07-02",
        "to_date": "1998-07-02"
      },
      {
        "salary": 45659,
        "from_date": "1998-07-02",
        "to_date": "1999-07-02"
      },
      {
        "salary": 46077,
        "from_date": "1999-07-02",
        "to_date": "2000-07-01"
      },
      {
        "salary": 48065,
        "from_date": "2000-07-01",
        "to_date": "2001-07-01"
      },
      {
        "salary": 49474,
        "from_date": "2001-07-01",
        "to_date": "2002-07-01"
      },
      {
        "salary": 50059,
        "from_date": "2002-07-01",
        "to_date": "9999-01-01"
      }
    ],
    "departments": [
      {
        "dept_no": "d005",
        "dept_name": "Development",
        "from_date": "1994-07-03",
        "to_date": "9999-01-01",
        "dept_manager": [
          {
            "emp_no": 110511,
            "first_name": "DeForest",
            "last_name": "Hagimont",
            "email": "110511@cloud-spartan.com",
            "from_date": "1985-01-01",
            "to_date": "1992-04-25"
          },
          {
            "emp_no": 110567,
            "first_name": "Leon",
            "last_name": "DasSarma",
            "email": "110567@cloud-spartan.com",
            "from_date": "1992-04-25",
            "to_date": "9999-01-01"
          }
        ]
      }
    ],
    "title": [
      {
        "title": "Engineer",
        "from_date": "1994-07-03",
        "to_date": "2000-07-02"
      },
      {
        "title": "Senior Engineer",
        "from_date": "2000-07-02",
        "to_date": "9999-01-01"
      }
    ]
  },

Console after JSON.parse():

{ 'body-json':
   { emp_no: 80000,
     email: '80000@cloud-spartan.com',
     first_name: 'Odoardo',
     last_name: 'Ranft',
     birth_date: '1963-06-23',
     gender: 'F',
     hire_date: '1994-07-03',
     salaries:
      [ [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object] ],
     departments: [ [Object] ],
     title: [ [Object], [Object] ] },
1 Answers
Best Answer
AvatarAmit answered 5 months ago
Your Answer

17 + 18 =

Popular Tags

WP Facebook Auto Publish Powered By : XYZScripts.com