shaun
shaun

Reputation: 1273

How do I console.log one value of this JSON Object?

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
  }
}

Upvotes: 1

Views: 1214

Answers (4)

Uthman
Uthman

Reputation: 9817

You have two main ways of doing it. Both are correct ways. Lets say your object is named obj. Use console.log as following:

  1. console.log(obj.fields.autonum)
  2. console.log(obj['fields']['autonum'])

First case is easier as compare to second case. Second case is safer as it will allow you to even take care of keys which have spaces e.g.

var my_other_object = {
   'Santa Clara': 'USA',
   'Toronto': 'Canada'
};

console.log(my_other_object['Santa Clara']) // Output will be 'USA'

For your understanding, in above object 'Santa Clara' and 'Toronto' are called 'keys' of my_other_object and 'USA', 'Canada' are called 'values' of those 'keys'.

So JSON object is essentially combination of key:value pairs.

P.S. Never apologize while asking a question all questions are valid but it is good to always search before asking. Still, people are always happy to help here. We all have went through same phases. :)

Upvotes: 1

oKonyk
oKonyk

Reputation: 1476

Lets say your object name var data, then data.fields.autonum would give us value 121

console.log(data.fields.autonum)

Just FYI:

As it is stated on MDN website:

some JavaScript is not JSON, and some JSON is not JavaScript

Just in order to improve understanding of vocabulary, 'JSON Object' is not a thing in our context ... it is

a syntax for serializing objects

Upvotes: 0

Hectron
Hectron

Reputation: 303

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);

Upvotes: 2

Ruben Vincenten
Ruben Vincenten

Reputation: 907

You'd use the following code, assuming the json object is called record:

console.log(record.fields.autonum);

Upvotes: 1

Related Questions