Skip to content

xdevplatform/5-ways-to-convert-json-to-csv

Repository files navigation

5 ways to convert a JSON object to a CSV v2

While working with APIs, one often finds themselves needing to convert a JSON object to a CSV. These code samples correspond to this blog post which will walk you through 5 strategies to turn a JSON response from a request you'll make using the recent search endpoint from v2 of the Twitter API.

What you need to get started

Twitter API Essential Access (sign up here). You will also need a bearer token from your App in the Twitter developer portal.

Setting up your environment variable

For all the code samples except R, you will first need to set up an environment variable in your terminal to save your bearer token to your request. You will need to replace your-bearer-token with your own bearer token. Be sure to keep the single quotes around your bearer token.

export BEARER_TOKEN='your-bearer-token'

For R, in the your console you will need to run the following line.

Sys.setenv(BEARER_TOKEN = "your-bearer-token")

Code for each of the 5 ways

  1. From the command line using csvkit
  2. Using the web
  3. Using requests and pandas in Python
  4. In R using httr
  5. In Node.js using axios and json2csv

Contributing

We welcome pull requests that add meaningful additions to these code samples.

We believe that a welcoming community is important and we ask that you follow Twitter's Open Source Code of Conduct in all interactions with the community.

License

Copyright 2020 Twitter, Inc.

Licensed under the Apache License, Version 2.0: https://www.apache.org/licenses/LICENSE-2.0