Skip to content

Consider accepting "format=csv" #208

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
krivard opened this issue Sep 4, 2020 · 3 comments
Closed

Consider accepting "format=csv" #208

krivard opened this issue Sep 4, 2020 · 3 comments

Comments

@krivard
Copy link
Contributor

krivard commented Sep 4, 2020

The API is very inconvenient for one of our users because they don’t use R or Python and they’re literally running API queries manually, then running the JSONs they find through online converters to get CSVs.

For now, we can put up a python server somewhere and have it do the transformation as a middleman, to make their workflow a little less precarious.

Long-term we should consider supporting CSV formatted output directly. What might make it tricky is the tight integration with the rest of Epidata, because this is how api.php currently ends:

// send the response as a json object
header('Content-Type: application/json');
echo json_encode($data);
?>
@korlaxxalrok
Copy link
Contributor

@krivard Do you have an example of how they are calling the API?

Assuming something like cURL to make requests, this is a cool example of using jq to format responses in CSV.

You'd pretty much pipe into jq with this bit of magic:

jq -r '.epidata | (map(keys) | add | unique) as $cols | map(. as $row | $cols | map($row[.])) as $rows | $cols, $rows[] | @csv'

An ex. against our API (truncated a wee bit):

❯ curl -s -X GET "https://delphi.cmu.edu/epidata/api.php?source=covidcast&cached=true&data_source=jhu-csse&signal=confirmed_incidence_num&geo_type=county&time_values=20200501&time_type=day&geo_value=*&as_of=20200509" | jq -r '.epidata | (map(keys) | add | unique) as $cols | map(. as $row | $cols | map($row[.])) as $rows | $cols, $rows[] | @csv'
"direction","geo_value","issue","lag","sample_size","stderr","time_value","value"
0,"01001",20200507,6,,,20200501,-2
0,"01003",20200507,6,,,20200501,1
0,"01005",20200507,6,,,20200501,3
0,"01007",20200507,6,,,20200501,0
0,"01009",20200507,6,,,20200501,2
0,"01011",20200507,6,,,20200501,1
0,"01013",20200507,6,,,20200501,12
0,"01015",20200507,6,,,20200501,-1
0,"01017",20200507,6,,,20200501,-5
0,"01019",20200507,6,,,20200501,0
0,"01021",20200507,6,,,20200501,-1
...

Redirected to a file and viewed in something fancy:
image

@krivard
Copy link
Contributor Author

krivard commented Sep 5, 2020 via email

@krivard
Copy link
Contributor Author

krivard commented Sep 8, 2020

Fixed in #210

@krivard krivard closed this as completed Sep 8, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants