Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to parse JSON DIC?
#1
Hello I'm trying to parse the JSON DIC, to get only the values of each item of the response and save them in a mysql table.
Does anybody can help me please?

Regards,
Orlando Gautier

import hmac
import time
import hashlib
import requests
import pickle
import json
from urllib.parse import urlencode
import mysql.connector
from mysql.connector import Error
from datetime import date
from datetime import datetime


mydb = mysql.connector.connect(
host="localhost",
user="xxxx",
password="xxxxx",
database="xxxxx"
)

KEY = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
SECRET = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'

# BASE_URL = 'https://fapi.binance.com' # production base url
BASE_URL = 'https://fapi.binance.com' # testnet base url
#timestamp = '1598295228'

def hashing(query_string):
    return hmac.new(SECRET.encode('utf-8'), query_string.encode('utf-8'), hashlib.sha256).hexdigest()

def get_timestamp():
    return int(time.time() * 1000)

def dispatch_request(http_method):
    session = requests.Session()
    session.headers.update({
        'Content-Type': 'application/json;charset=utf-8',
        'X-MBX-APIKEY': KEY
    })
    return {
        'GET': session.get,
        'DELETE': session.delete,
        'PUT': session.put,
        'POST': session.post,
    }.get(http_method, 'GET')


def send_signed_request(http_method, url_path, payload={}):
    query_string = urlencode(payload)
    #Reemplazar comillas simples por comillas dobles
    query_string = query_string.replace("%27", "%22")
    if query_string:
        query_string = "{}&timestamp={}".format(query_string, get_timestamp())
    else:
        query_string = "timestamp={}".format(get_timestamp())

    url = BASE_URL + url_path + '?' + query_string + '&signature=' + hashing(query_string)
    print("{} {}".format(http_method, url))
    params = {'url': url, 'params': {}}
    response = dispatch_request(http_method)(**params)
    return response.json()

def send_public_request(url_path, payload={}):
    query_string = urlencode(payload, True)
    url = BASE_URL + url_path
    if query_string:
        url = url + '?' + query_string
    print("{}".format(url))
    response = dispatch_request('GET')(url=url)
    return response.json()

''' ======  end of functions ====== '''

params = {
    "symbol": "SXPUSDT",
    "side": "BUY",
    "type": "LIMIT",
    "timeInForce":"GTC",
    #"leverage": 10,
    #"maxNotionalValue": "1000",
    #"dualSidePosition": "false",
    "quantity": 7,
    #"origQty": "10",
    "price": 1.7560,
    "positionSide": "LONG",    
    "recvWindow":"20000"
    
}
response = send_signed_request('POST', '/fapi/v1/order', params)
print(response)


if response != 0:
    #print("There are data",response)
    mycursor=mydb.cursor()      
    sql=("INSERT INTO TABLE(orderId,symbol,status,clientOrderId) VALUES(%s,%s,%s,%s)",(response['orderId'],response['symbol'],response['status'],response['clientOrderId']))   
    mycursor.execute(sql)    
else:
    print("There are no data")
sample json
Output:
{"orderId": 232501486, "symbol": "SXPUSDT", "status": "NEW", "clientOrderId": "EYn1aQbo5jMJNMCjxxpshq", "price": "1.7999", "avgPrice": "0.00000", "origQty": "7", "executedQty": "0", "cumQty": "0", "cumQuote": "0", "timeInForce": "GTC", "type": "LIMIT", "reduceOnly": false, "closePosition": false, "side": "BUY", "positionSide": "LONG", "stopPrice": "0", "workingType": "CONTRACT_PRICE", "priceProtect": "false", "origType": "LIMIT", "updateTime": 1600173879564}
Reply
#2
you want
sql= "INSERT INTO TABLE(orderId,symbol,status,clientOrderId) VALUES(%s,%s,%s,%s)"   
mycursor.execute(sql, (response['orderId'],response['symbol'],response['status'],response['clientOrderId']))
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Hello buran,

I´m sorry, and than you.

Yes, I want to save in mysql table, the result of that JSON response.

Best Regards,

Orlando Gautier
Reply
#4
does it solve your problem?
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
Yes Buran...it solve the problem.

Thank you very much!!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  parse json field from csv file lebossejames 4 764 Nov-14-2023, 11:34 PM
Last Post: snippsat
  [split] Parse Nested JSON String in Python mmm07 4 1,540 Mar-28-2023, 06:07 PM
Last Post: snippsat
  Trying to parse only 3 key values from json file cubangt 8 3,495 Jul-16-2022, 02:05 PM
Last Post: deanhystad
  Problem to parse a json enigma619 3 2,392 Dec-04-2020, 08:16 AM
Last Post: enigma619
  Parse JSON multiple objects larkin_L 8 5,752 May-27-2020, 11:18 AM
Last Post: nuffink
  How can i parse a log file to JSON. menarcarlos 2 2,440 May-26-2020, 10:23 AM
Last Post: buran
  can't parse json file jolinchewjb 1 2,340 Jan-25-2019, 09:54 AM
Last Post: Larz60+
  parse tree to json dictionary in python maryum 0 2,606 Aug-27-2018, 04:59 AM
Last Post: maryum
  Unable to parse JSON output dragan979 1 3,554 Apr-20-2018, 02:24 PM
Last Post: dragan979

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020