How to convert data into JSON dictionary?

dict to json python
python json to dict example
convert dictionary to json swift
python create json array
extract fields from json python
python extract data from json file
python list to json
json formatter

So I get a response and print it. The result is bytes:

payload = request.body
print (payload)
b'a=123&b=345&c=678&d=910'

I decode it, and the result is:

dataform = payload.decode('utf-8').replace("'", '"')
print(dataform, 'dataform')
a=123&b=345&c=678&d=910

I dumps it, and the result is:

result = json.dumps(dataform, indent=4, sort_keys=True)
print(result, 'result')
"a=123&b=345&c=678&d=910"

I loads it, and the result is:

jason = json.loads(result)
print(jason, 'jason')
a=123&b=345&c=678&d=910

I just want a normal json dictionary that I can refer to like data['string']. What am I doing wrong or not doing?

There's a few errors here.

First off, dumping to JSON and then loading it again does absolutely nothing (it does have a few side-effects, but that's not important here).

Secondly, and mainly, your input data isn't JSON - it's either a query string or, more likely, form-data.

You can try to parse it using the standard parse_qs in urllib.parse, but if that fails you'll have to look around for a library that can handle proper form data.

In [1]: from urllib.parse import parse_qs

In [2]: payload = b'a=123&b=345&c=678&d=910'

In [3]: dataform = payload.decode('utf-8').replace("'", '"')

In [4]: result = parse_qs(dataform)

In [5]: print(result)
{'a': ['123'], 'b': ['345'], 'c': ['678'], 'd': ['910']}

Convert Dictionary to JSON in Swift, prettyPrinted) // here "jsonData" is the dictionary encoded in JSON data let decoded = try JSONSerialization.jsonObject(with: jsonData, options: []) // here  Learn how to extract and analyze JSON data in the cloud without writing SQL. JSON is a potential treasure trove for companies that can harness it effectively

At first, you need to convert the string (here, as the example, to the array, but you can use that you want)

data = [x.split('=') for x in data.split('&')]
>>> data
[['a', '123'], ['b', '345'], ['c', '678'], ['d', '910']]

And after this, you can easily create the dictionary.

dict = {key: value for (key,value) in data}
>>> dict
{'a': '123', 'c': '678', 'b': '345', 'd': '910'}

Or if you want to store numbers as int:

dict = {key: int(value) for (key,value) in data}
>>> dict
{'a': 123, 'c': 678, 'b': 345, 'd': 910}

Python dict to json, loads() method convert into JSON. You can check out the How to Parse JSON in Python. JSON (JavaScript Object Notation) is the popular data  Warning: this is a convenience method to convert a JSON string to a dictionary if, for some reason, you have to work from a JSON string. But if you have the JSON data available, you should instead work with the data, without using a string at all.

import json
from urllib.parse import parse_qs

payload = request.body
# b'a=123&b=345&c=678&d=910'

qs = parse_qs(payload.decode())
# {'a': ['123'], 'b': ['345'], 'c': ['678'], 'd': ['910']}

Convert list values and convert data into JSON

json.dumps({k: v[0] for k, v in qs.items()})
# '{"a": "123", "b": "345", "c": "678", "d": "910"}'

How To Convert Python Dictionary To JSON?, It means that a script (executable) file which is made of text in a programming language, is used to store and transfer the data. Python supports JSON through a​  Now, you can convert a dictionary to JSON string using the json.dumps() method. The process of encoding the JSON is usually called the serialization.That term refers to the transformation of data into the series of bytes (hence serial) to be stored or transmitted across the network.

Convert JSON to dictionary in Python, Python program to demonstrate. # Conversion of JSON data to. # dictionary. # importing the module. import json. # Opening JSON file. with open ( 'data.json' )  In this tutorial, we will learn how to convert the JSON (JavaScript Object Notation) string to the Python dictionary. JSON is a popular data format used for data manipulation. To learn creating a dictionary from JSON carry on reading this article… Python program to convert JSON string to Dictionary

How to use JSON with Python, One of the reasons JSON might be used is to collect data from the used to convert the python dictionary above into a JSON string that can be  Note that that is not valid JSON: you have to use double-quotes, not single-quotes. Assuming you fix that, and that the JSON has already been retrieved into a string variable jsonResult, you need to parse that to get an object using JSON.parse(): var result = JSON.parse(jsonResult); You can build a dictionary from there:

Converting dictionary to JSON, I am not able to access my data in the JSON. What am I doing wrong? TypeError: string indices must be integers, not str. python  Stack Overflow Public questions and Converting JSON objects in to dictionary in python. No my requirement is to convert this to JSON objects as a dictionary

Comments
  • The string doesn't contain any single quotes, so replacing them is superfluous (and if it wasn't, it'd be wrong). Other than that, are you looking for d = dict(x.split('=', 1) for x in payload.decode('utf-8').split('&')) perhaps?
  • The .replace() part is superfluous. Also in python2, you should do from urlparse import parse_qs.
  • the parse_qs is what I needed. Thanks. I did not know of that.
  • An explanation, what the posted code does and how this addresses the problem in the question, rarely fails to improve an answer.