1

I'm trying to convert bytes data into JSON data. I got errors in converting data.

a = b'orderId=570d3e38-d6486056e&orderAmount=10.00&referenceId=34344&txStatus=SUCCESS&txMsg=Transaction+Successful&txTime=2021-06-26+12%3A03%3A12&signature=njtH5Dzmg6RJ1KB'

I used this to convert

json.loads(a.decode('utf-8'))

and I want to get a response like

orderAmount = 10.00
orderId = 570d3e38-d6486056e
txStatus = SUCCESS

1
  • 1
    It looks like you just want to convert a query string to a dictionary? Where did you get that? Normally a query string passed to Django would be accessible as a QueryDict which can be accessed by request.GET Commented Jun 26, 2021 at 7:01

1 Answer 1

1

What you here see is a query string [wiki], you can read the querystring with:

from django.http import QueryDict

a = b'orderId=570d3e38-d6486056e&orderAmount=10.00&referenceId=34344&txStatus=SUCCESS&txMsg=Transaction+Successful&txTime=2021-06-26+12%3A03%3A12&signature=njtH5Dzmg6RJ1KB'

b = QueryDict(a)

with the given sample data, we have a QueryDict [Django-doc] that looks like:

>>> b
<QueryDict: {'orderId': ['570d3e38-d6486056e'], 'orderAmount': ['10.00'], 'referenceId': ['34344'], 'txStatus': ['SUCCESS'], 'txMsg': ['Transaction Successful'], 'txTime': ['2021-06-26 12:03:12'], 'signature': ['njtH5Dzmg6RJ1KB']}>

If you subscript this, then you always get the last item, so:

>>> b['orderId']
'570d3e38-d6486056e'
>>> b['orderAmount']
'10.00'
>>> b['orderId']
'570d3e38-d6486056e'
>>> b['txStatus']
'SUCCESS'

If you work with a view, you can also find this querydict of the URL with:

def my_view(request):
    b = request.GET
    # …
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.