0

Below is a extract of a dict going through JSON.

["1", "ABC", "new Date(2015/01/01)", "new Date(2015/08/07)"], etc

Is there any way to remove the double quotes on only the dates like below:

["1", "ABC", new Date(2015/01/01), new Date(2015/08/07)], etc

Edit

dataset = []
for service in ServiceData:
        datestart = 'new Date('+service.DateCommence+')'
        dateend = 'new Date('+service.DateDiscontinued+')'
        dataset.append([
                        "{}".format(i),
                        service.Title,
                        datestart,
                        dateend,
                        ])
        i = i + 1
return json.dumps(dataset)
BAW331
  • 53
  • 1
  • 1
  • 9
  • 1) That's not "a dict', it is a JSON array. 2) Why do you want to do this? What is the background of your question? –  Jun 05 '15 at 10:32
  • Hi Lutz, im trying to format it to use with google charts timeline. They require it to be in this format [ 'Washington', new Date(1789, 3, 29), new Date(1797, 2, 3) ]. I just need to remove the double quotes around the dates and it should be ready to go. Thanks for the help. – BAW331 Jun 05 '15 at 10:35
  • Where do you get this input from? You say it is JSON. Why does it contain code (`"new Data(2015/08/07)"`) and not a proper datetime string? –  Jun 05 '15 at 10:37
  • Please see edited form above. This is how the json array is created. Then this array is used with google chart time to create the graph. – BAW331 Jun 05 '15 at 10:41
  • 1
    You mean new Date() when you write new Data() right? Please make the edit – Namit Singal Jun 05 '15 at 10:46

3 Answers3

0

If you want to put a date into JSON, use the standard ISO 8601:

["1", "ABC", "2015-01-01", "2015-08-07)"]

If service.DateCommence produces dates as 2015/08/07, you should convert this input to ISO dates:

datestart = service.DateCommence.replace("/", "-")

Every client or library that consumes JSON should be able to understand this date format.

0

First change the backend code to

["1", "ABC", "new Date(2015/01/01)", "new Date(2015/08/07)"]

Here I changed Data to Date

ok, I don't think this is the best way, but you don't need to change anything on the backend, one of the hacks which could work is this, on frontend, change the js code and edit the json that you got from backend by iterating through it, again this is not the best way.

for(index in data){
    if(data[index].startsWith("new Date")){
        data[index]=eval(data[index]);
    }
}

if you know the exact index, just do

data[2] = eval(data[2]);
data[3] = eval(data[3]);

assuming index 2 and 3 have datetype object strings. Also run these steps through the full list. cheers

Namit Singal
  • 1,506
  • 12
  • 26
  • Recommending `eval` over fixing the backend code is not very good advise. Please read http://stackoverflow.com/q/1087255/1907906 –  Jun 05 '15 at 12:38
0

You can avoid this as Google Charts provides means to supply a string representation of a date. This has the advantage of allowing you to use valid JSON, which will make using json.dumps much easier:

From the docs, the string has this format:

"Date(Year, Month, Day, Hours, Minutes, Seconds, Milliseconds)"

You must also specify the column as type 'date' for the value to be interpreted correctly.

In Python you can generate such a string like this:

"Date({year}, {month}, {day}, {hour}, {minute}, {second})".format(
    year=date.year,
    month=date.month - 1,
    day=date.day,
    hour=date.hour,
    minute=date.minute,
    second=date.second
)
starcross
  • 13
  • 5