Python Forum

Full Version: Reducing JSON character count in Python for a Twitter Bot
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Redacted - Please delete.
Adding some more structure here to see if someone can help.

I have made some changes and started over so it may look a little different than previous code.

import tweepy
import time
#from time import sleep | May not need this line if importing entire time lib
import json
import requests
from credentials import *
import schedule

url = 'https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest'

#access and authorize our Twitter credentials from credentials.py

auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
api = tweepy.API(auth)

headers = {
    'X-CMC_PRO_API_KEY' : 'redacted',
    'Accepts' : 'application/json',
}

params = {
    'start' : '1',
    'limit' : '10',
    'convert' : 'USD',
}

json = requests.get(url, params=params, headers=headers).json()
print(json)

coins = json['data']
print("Top 10 Crypto by Market Cap")

for coin in coins:
    #del coin['timestamp']['error_code']['credit_count']['notice']['elapsed']['errormessage']['total_count']['data']['id']
    print("Symbol:", coin['symbol'])
    #,"Price:","$",round(coin['quote']['USD']['price'],2))
    coin = str(coins)
    char_count = len(coin)
    print(char_count)
From OP, I'd like to take this output (minus the char count displayed below each):

Top 10 Crypto by Market Cap
Symbol: BTC
9891
Symbol: ETH
9891
Symbol: BNB
9891
Symbol: XRP
9891
Symbol: USDT
9891
Symbol: ADA
9891
Symbol: DOGE
9891
Symbol: DOT
9891
Symbol: UNI
9891
Symbol: LTC
9891

The goal would be to feed this data through the Tweepy API and auto post this information on a time schedule every day. When I feed this through the API, I get a an error that I have exceeded the character count, which is why I added the print to include the char count for each coin.

Something tells me that with each query to the CMC API, it is attaching a ton more data packaged into the output.

The total character count (minus the char counter information) is 149 including spaces, so theoretically it should work.

Do I need to decode the JSON data or delete irrelevant information from the data set before outputting?

I didn't post the error for the tweeps bot because it's irrelevant at this point, but can update if need be.

Also, this program is an overly simplified version of what my ultimate goal for the program is, but this is my starting point.

Thanks in advance for any help you guys can provide.
Mods, please delete.

I figured it out.