Seong Lee
Seong Lee

Reputation: 10520

Parsing JSON to object using JSON.parse

I have the following response in JSON from Youtube Data API

{
 "kind": "youtube#channelListResponse",
 "etag": "\"0KG1mRN7bm3nResDPKHQZpg5-do/B7stMlWJTBpmW2q34yWKIzz8fF8\"",
 "pageInfo": {
  "totalResults": 1,
  "resultsPerPage": 1
 },
 "items": [
  {
   "kind": "youtube#channel",
   "etag": "\"0KG1mRN7bm3nResDPKHQZpg5-do/vV2FFZUI5inz53NuQDJMTs3tdQk\"",
   "id": "UCwy6X3JB24VTsDFqMwdO5Jg",
   "contentDetails": {
    "relatedPlaylists": {
     "uploads": "UUwy6X3JB24VTsDFqMwdO5Jg"
    },
    "googlePlusUserId": "114467711950028252332"
   }
  }
 ]
}

I'm trying to turn it into an object using JSON.parse but doing so gives me this.

{ kind: 'youtube#channelListResponse',
  etag: '"0KG1mRN7bm3nResDPKHQZpg5-do/B7stMlWJTBpmW2q34yWKIzz8fF8"',
  pageInfo: { totalResults: 1, resultsPerPage: 1 },
  items: 
   [ { kind: 'youtube#channel',
       etag: '"0KG1mRN7bm3nResDPKHQZpg5-do/vV2FFZUI5inz53NuQDJMTs3tdQk"',
       id: 'UCwy6X3JB24VTsDFqMwdO5Jg',
       contentDetails: [Object] } ] }

How can I turn the value of contentDetails into a proper object?

Upvotes: 0

Views: 1649

Answers (3)

Kaicui
Kaicui

Reputation: 3863

your can use JSON.stringify(parsedObject) to see the full details

Upvotes: 0

Oleg Sklyar
Oleg Sklyar

Reputation: 10082

It is already a proper object, it is just not output correctly onto the console. Try printing the result of JSON.parse, e.g. res using the following:

console.log(util.inspect(res, { showHidden: true, depth: null }));

See https://nodejs.org/api/util.html#util_util_inspect_object_options

Upvotes: 3

NguyenNgocPhuong
NguyenNgocPhuong

Reputation: 437

it's a proper object, You can still access it's properties in normal way. Because console.log doesn't print deep level object so if you want to see it on the log, let convert it into a string. Try object to string script here https://stackoverflow.com/a/5612876

Upvotes: 0

Related Questions