converting Flask message received in in "text/plain" format to JSON format

Question:

I have an issue with FLASK that I am hoping to resolve.

I am sending this type of input to a Flask server:

[
    "tradeid": "5L5A0",
    "message": "",
    "accountid": 20,
    "ticker": {{ticker}},
    "currentprice": {{close}},
    "action": "X_OPEN"
]

I have also tried (changing ] to } ):

{
    "tradeid": "5L5A0",
    "message": "",
    "accountid": 20,
    "ticker": {{ticker}},
    "currentprice": {{close}},
    "action": "X_OPEN"
}

The server does change {{ticker}} and {{close}} values so that they are "filled" with actual data ( as seen here in the debugger):

(Pdb) p request.data
b'[n "tradeid": "5L5A0",n "message": "",n "accountid": 20,n "ticker": BTCUSDT,n "currentprice": 19091.4,n "action": "X_OPEN"n]’

The snippet of code looks like this:
671     @tview_api.route('/record_alerts', methods=['GET', 'POST'])
672     def save_tview_alerts( ):
673         try:
674             breakpoint()
675             #req_data = request.get_json()
676  ->         req_data = request.data

The problem is that it is sending it as plain text and not json (otherwise request.get_json() would work).

How can I convert this result to JSON format?

Any help, hints or advice would be greatly appreciated

TIA

UPDATE:

Thanks to all who have responded. I have been asked if I could send the data as : "application/json" I am using a Webhook from Tradingview. They have informed me that "they are working on it" – but – in the meantime, I was trying to find a workaround.

Again, any help, hints or advice would be appreciated

Background message is here: Tradingview client sending Json structure as plain text/data

Asked By: Casey Harrils

||

Answers:

First and foremost, this isn’t valid JSON syntax:

[
 "tradeid": "5L5A0",
 "message": "",
 "accountid": 20,
 "ticker": BTCUSDT,
 "currentprice": 19091.4,
 "action": "X_OPEN"
]

The [...] delimiters are for specifying a list, something like:

["one", "two", "three"]

If you want key: value items, you need a dictionary (called an "object" in JSON parlance):

{
 "tradeid": "5L5A0",
 "message": "",
 "accountid": 20,
 "ticker": BTCUSDT,
 "currentprice": 19091.4,
 "action": "X_OPEN"
}

Except that’s still not valid syntax, because BTCUSDT isn’t a valid JSON value: it’s neither a string, nor a number, nor a boolean value, nor null. You need to fix that:

{
  "tradeid": "5L5A0",
  "message": "",
  "accountid": 20,
  "ticker": "BTCUSDT",
  "currentprice": 19091.4,
  "action": "X_OPEN"
}

Using the above data and this code:

from flask import Flask, request, make_response

tview_api = Flask(__name__)


@tview_api.route("/record_alerts", methods=["GET", "POST"])
def save_tview_alerts():
    req_data = request.get_json(force=True)
    return req_data

I can POST the above data without errors.

You need to update your template to look like:

{
    "tradeid": "5L5A0",
    "message": "",
    "accountid": 20,
    "ticker": "{{ticker}}",
    "currentprice": {{close}},
    "action": "X_OPEN"
}

Because I’m using request.get_json(force=True) in the above code, we don’t care about the Content-type header of the request. If you can ensure you’re using the appropriate Content-type header (application/json), then you could simply reference the request.json attribute.

If you are unsure about JSON syntax, you can utilize a JSON validator like this one to check your data. For example, attempting to validat
e your dictionary with the unquoted string, that site tells us:

Error: Parse error on line 5:
...id": 20, "ticker": BTCUSDT,  "currentpr
----------------------^
Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '[', got 'undefined'
Answered By: larsks
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.