Keshav Vasudevan Keshav Vasudevan - 7 months ago 10
Javascript Question

Modify obtained JSON data

I recently was introduced to the world of APIs, and I'm trying to wrap my head around it a bit more, so thank you all in advance!

I have the following JSON data:

[
{
"description": "Batman does.\n\nBatman vs. The Penguin (with Patton Oswalt)",
"updated_time": "2016-03-29T16:35:00+0000",
}
]


Doing something like

var var1 = array.[0]description


will store var1 as the whole description.

I use Javascript (JQuery specifically). I want to get only the description before the
\n\
i.e Batman does., and store it in a variable.

Answer
  1. Split on the newline and take the first array item, eg

    var desc = data[0].description.split('\n')[0];
    
  2. Parse the date string into a Date instance. Seeing as your string is ISO 8601 compliant, you can simply use the Date constructor...

    var d = new Date(data[0].updated_time);
    

    See formatting options here ~ https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Date#Conversion_getter

Comments