# How to convert a QTable to JSON

**URL:** <https://community.openastronomy.org/t/how-to-convert-a-qtable-to-json/682>\
**Category:** Astropy\
**Tags:** astropy, question\
**Created:** [June 3, 2023, 7:40am UTC](https://community.openastronomy.org/t/how-to-convert-a-qtable-to-json/682 "2023-06-03T07:40:21Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ziriuz84](https://dub1.discourse-cdn.com/flex005/user_avatar/community.openastronomy.org/ziriuz84/32/378_2.png) [@ziriuz84](https://community.openastronomy.org/u/ziriuz84)\
**Post date:** [June 3, 2023, 7:40am UTC](https://community.openastronomy.org/t/how-to-convert-a-qtable-to-json/682/1 "2023-06-03T07:40:21Z")

</div>

Hi all!

I’m trying to convert a QTable to a json object thorugh [this function](https://pastebin.com/hH22bsMW). I have this error `TypeError: Object of type Time is not JSON serializable`.  
I think I must convert the **Date** column to some iso format, but I don’t know how.

Any help?

---

<div class="post-metadata">

**Author:** ![hamogu](https://dub1.discourse-cdn.com/flex005/user_avatar/community.openastronomy.org/hamogu/32/93_2.png) [@hamogu](https://community.openastronomy.org/u/hamogu)\
**Post date:** [June 10, 2023, 11:48pm UTC](https://community.openastronomy.org/t/how-to-convert-a-qtable-to-json/682/2 "2023-06-10T23:48:14Z")

</div>

You don’t say if your Time is an `Time` is an [astropy Time object](https://docs.astropy.org/en/stable/api/astropy.time.Time.html#astropy.time.Time) or a Python [datetime](https://docs.python.org/3/library/datetime.html). I’m assuming the former, since you pasted this under astropy, but if not the same idea applies:

The time object internally has all sorts of metadata (e.g. the format for output for string) and does not know how to put that into json. So, it needs a little help. You can manually convert your time into a format that’s easier to save a json, e.g. an ISO time as a string or as a number in JD. What format is best for you depends on your application, so I’m just giving one example, see [the astropy docs](https://docs.astropy.org/en/stable/time/index.html) for more formats that a time or date can converted to.

```auto
import json
import numpy as np
from astropy.time import Time
from astropy.table import QTable
import astropy.units as u

times = ['1999-01-01T00:00:00.123456789', '2010-01-01T00:00:00']
t = Time(times, format='isot', scale='utc')

tab = QTable({'date': t, 'mag': [15, 15.6] * u.mag})
json.dumps({'date': tab['date'].jd.tolist(), 
            'mag': tab['mag'].value.tolist()})

```

However, note that you get all the numbers, but loose the metadata (e.g. the units) from your table. Depending on what you need the json for, that _might_ be OK, but in other cases, a lossless format could be more useful, e.g. `tab.write('filename.ecsv')`.
