Ad

How Do I Console.log One Value Of This JSON Object?

- 1 answer

I am new to javascript and to JSON, so please forgive me. If I have the following JSON Object, how would I console.log() out the value of autonum?

 {
  "database": "testdb",
  "table": "path",
  "affectedColumns": [
    {
      "name": "autonum",
      "charset": null,
      "type": 8
    },
    {
      "name": "TimeStamp",
      "charset": null,
      "type": 18,
      "metadata": {
        "decimals": 0
      }
    },
    {
      "name": "FilePath",
      "charset": "latin1",
      "type": 15,
      "metadata": {
        "max_length": 256
      }
    },
    {
      "name": "DirPath",
      "charset": "latin1",
      "type": 15,
      "metadata": {
        "max_length": 256
      }
    },
    {
      "name": "DirName",
      "charset": "latin1",
      "type": 15,
      "metadata": {
        "max_length": 256
      }
    },
    {
      "name": "EventName",
      "charset": "latin1",
      "type": 15,
      "metadata": {
        "max_length": 256
      }
    },
    {
      "name": "FileName",
      "charset": "latin1",
      "type": 15,
      "metadata": {
        "max_length": 256
      }
    },
    {
      "name": "FileExt",
      "charset": "latin1",
      "type": 15,
      "metadata": {
        "max_length": 10
      }
    },
    {
      "name": "FileSize",
      "charset": null,
      "type": 3
    },
    {
      "name": "MainFlag",
      "charset": null,
      "type": 1
    },
    {
      "name": "DeleteFlag",
      "charset": null,
      "type": 1
    },
    {
      "name": "Status",
      "charset": "latin1",
      "type": 15,
      "metadata": {
        "max_length": 255
      }
    },
    {
      "name": "ProcessedFlag",
      "charset": null,
      "type": 1
    }
  ],
  "changedColumns": [],
  "fields": {
    "autonum": 121,
    "TimeStamp": "2016-01-13T00:21:13.000Z",
    "FilePath": "c:/1E0304F120151223030158001.mp4",
    "DirPath": "c:\\",
    "DirName": null,
    "EventName": null,
    "FileName": "1E0304F120151223030158001.mp4",
    "FileExt": ".mp4",
    "FileSize": 2218108,
    "MainFlag": 0,
    "DeleteFlag": 0,
    "Status": null,
    "ProcessedFlag": 0
  }
}
Ad

Answer

Depending on which autonum you want, and assuming your variable storing the JSON is data, you'll want to do something as follows:

console.log(data.fields.autonum);

or

console.log(data.affectedColumns[0].name);
Ad
source: stackoverflow.com
Ad