You can use the json.dumps()
function with the separators
parameter to eliminate white spaces in a JSON file generated by Python.
The separators
parameter is a tuple that specifies the characters used to separate the items in the JSON string. The first character of the tuple represents the separator between items (usually a comma), while the second character represents the separator between keys and values (usually a colon).
Here's an example of how to use json.dumps()
with the separators
parameter:
import json
data = {"name": "John", "age": 30, "city": "New York"}
# Dump the data to a JSON string without white spaces
json_str = json.dumps(data, separators=(",", ":"))
# Print the JSON string
print(json_str)
Output:
{"name":"John","age":30,"city":"New York"}
As you can see, the resulting JSON string has no white spaces between the items.
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2023-07-12 00:03:41 +0000
Seen: 12 times
Last updated: Jul 12 '23
How can popen() be used to direct streaming data to TAR?
In Python, can a string be utilized to retrieve a dataframe that has the same name as the string?
What is the method for merging field value and text into a singular line for display?
What is the method for programmatic access to a time series?