1

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] ] },
Rajesh Gupta
  • 178
  • 1
  • 1
  • 8
mrsong822
  • 39
  • 5
  • Possible duplicate of [How can I get the full object in Node.js's console.log(), rather than '\[Object\]'?](https://stackoverflow.com/questions/10729276/how-can-i-get-the-full-object-in-node-jss-console-log-rather-than-object) – 1565986223 Apr 07 '19 at 04:52
  • Thanks for suggesting. My following question is how I can access some specific data? For example, I would like to retrieve only emp_no or salaries. – mrsong822 Apr 07 '19 at 04:54

2 Answers2

0

You a problem in your code, you do not need to stringfy before parse.

After parsing you can now access the emp_no like this: json_result_post_parse["body-json"].emp_no

Below is a working sample code:

const data = `{
  "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"
      }
    ]
  }
}`

function testjson() {
  const parsedData = JSON.parse(data);
  console.log(parsedData["body-json"].emp_no)
}

testjson()
Muhammad Altabba
  • 2,583
  • 19
  • 31
-1

To get a specific data you have to declare a variable or an object first. For example you want emp_no, then

var emp = body-json.emp_no

or if you want salaries then

var salary = body-json.salaries.salary
3rdsty4bl00d
  • 134
  • 10