You're escaping the inner double quote " in your string. It should be:

b"{\"Machine Name\":\""+hostname+"\"}", None, True)

In python you can also use single quotes ' for strings - and you don't need to escape double quotes inside single quoted strings

b'{"Machine Name":"'+hostname+'"}', None, True)

There are two better ways of doing this though. The first is string formatting which inserts a variable into a string:

b'{"Machine Name":"%s"}' % hostname # python 2.x (old way)
b'{{"Machine Name":"{0}"}}'.format(hostname) # python >= 2.6 (new way - note the double braces at the ends)

The next is with the Python JSON module by converting a python dict to a JSON string

>>> hostname = "machineA.host.com"
>>> data = {'Machine Name': hostname}
>>> json.dumps(data)
'{"Machine Name": "machineA.host.com"}'

This is probably the preferred method as it will handle escaping weird characters in your hostname and other fields, ensuring that you have valid JSON at the end.

Is there a reason you're using a bytestring

Answer from Peter Gibson on Stack Overflow
๐ŸŒ
Python Examples
pythonexamples.org โ€บ python-create-json
Python Create JSON
In this example, we will create JSON formatted string from a Python Tuple. import json myTuple = ({'a': 54}, {'b': 41, 'c':87}) jsonString = json.dumps(myTuple, indent=4) print(jsonString) ... In this Python JSON Tutorial, we learned how to create a JSON String from Python Objects, with the ...
Discussions

Generating Json file with custom variables Python - Stack Overflow
I'm trying to generate a json file preset with variables that are received from the user from input().The only method I found sorta-similar to mine is here, but it doesnt show how to generate a new... More on stackoverflow.com
๐ŸŒ stackoverflow.com
December 16, 2017
python - Create JSON object with variables from an array - Stack Overflow
I want to create a JSON object with an array and I can't seem to solve the problem. The problem I'm having is that it only assigns the last index value to my variable.Can someone show me how to ass... More on stackoverflow.com
๐ŸŒ stackoverflow.com
python - How to dynamically create a JSON string? - Stack Overflow
The values for the keys will be taken from various variables. For example we can take value "Sales" from a var called dbname with the value "Sales". I have tried json.load and getting exception. Help would be appreciated please as I am a bit new to Python. More on stackoverflow.com
๐ŸŒ stackoverflow.com
How to create a python variable from a json format - Stack Overflow
I want to create a global variable from json data. I'm sending the json data with websockets from a server to a client and I want that when the client receives json data, it creates a global variab... More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
YouTube
youtube.com โ€บ codinggpt
python create json string with variables - YouTube
Instantly Download or Run the code at https://codegive.com creating a json string with variables in python: a step-by-step tutorialjson (javascript object n...
Published: February 25, 2024
Views: 4
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ build-a-json-object-in-python
Build a Json Object in Python - GeeksforGeeks
July 23, 2025 - The dictionary is converted into o a JSON string json_string using json.dumps() with the custom encoder 'Encoder'. ... import json def encoder(obj): if isinstance(obj, set): return list(obj) return obj gfg = [('name', 'Hustlers'), ('age', 19), ...
Top answer
1 of 2
2

You just need to produce a python representation (lists + dicts etc.) of the structure you want and then use the json library to dump it to a file.

So in your case,

import json

# Get these from input
filename = "test.json"
width = 3
height = 5
placeholder = 1255255255

obj = {
    "width": width,
    "height": height,
    "column": [{row: placeholder for row in range(height)} for col in range(width)]
}

with open(filename, "w") as out_file:
    json.dump(obj, out_file)
2 of 2
1

I used list and dict comprehension to generate desired number of dictionaries with desired number of keys, then I used json.dump to serialize dictionary to JSON formatted string (while providing indent parameter, otherwise generated JSON would be just one line) and saved that string to the file opened with context manager (the preferred way to open files).

import json
import os

filename = input("Enter the name of the json file: ")
width = int(input("Enter the width: "))
height = int(input("Enter the height: "))

# Append .json if user did not provide any extension
if not os.path.splitext(filename)[1]:
    filename += ".json"

with open(filename, 'w') as f:
    json.dump({
        "width": width,
        "height": height,
        "column": [
            {
                str(row_idx): 0 for row_idx in range(height)
            }
            for column_idx in range(width)
        ]
    }, f, indent=4)

print("JSON saved to file {}".format(os.path.abspath(filename)))

Testing:

Enter the name of the json file: test_json
Enter the width: 2
Enter the height: 2
JSON saved to file C:\Users\Bojan\.PyCharm2017.3\config\scratches\test_json.json

Content of the test_json.json file:

{
    "width": 2,
    "height": 2,
    "column": [
        {
            "0": 0,
            "1": 0
        },
        {
            "0": 0,
            "1": 0
        }
    ]
}
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ python_json.asp
Python JSON
Remove List Duplicates Reverse a String Add Two Numbers ยท Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Practice Problems Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Training ... JSON is a syntax for storing and exchanging data. JSON is text, written with JavaScript object notation.
Find elsewhere
๐ŸŒ
Towards Data Science
towardsdatascience.com โ€บ home โ€บ latest โ€บ how to handle json in python
How to handle JSON in Python | Towards Data Science
January 16, 2025 - Note: Remember for the conversion of the Python objects into a JSON string you need to use dumps(). First, let us create four different types of variables which can hold the above python data
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 70959729 โ€บ how-to-create-a-python-variable-from-a-json-format โ€บ 70961235
How to create a python variable from a json format - Stack Overflow
There's no problem with the send/receive procedures, all the messages are sent and received between the client and the server. I just want to know if we can create a global variable from this. ... Save this answer. ... Show activity on this post. You can use the global keyword to modify a global variable from inside the function thar receives the data: Copyvariable_data = {} def receive_data(interface): global variable_data variable_data = interface.recv_json()
๐ŸŒ
Checkmk
forum.checkmk.com โ€บ troubleshooting
REST API: Python question: How to use variables in "json={" post in key and value? - Troubleshooting - Checkmk Forum
October 24, 2022 - CMK version:2.0.0p26 OS version:RHEL 8 Hello Python Experts: I need your help to add a server via REST API by using variables for the keys and values. In general it works but I need some help to get the right syntax for the key and value definitions by using variables.
๐ŸŒ
Team Treehouse
teamtreehouse.com โ€บ community โ€บ how-to-create-variables-based-on-a-json-file
How to create variables based on a json file ? (Example) | Treehouse Community
May 28, 2016 - JSONObject forecast = JSONObject(jsonData); String name = forecast.getString("Treehouse Book Series"); String publisher = forecast.getString("Wiley"); String language = forecast.getString("English"); ... { "name":"Treehouse Book Series", ...
๐ŸŒ
Quora
quora.com โ€บ How-do-I-save-JSON-data-as-variables-in-Python
How to save JSON data as variables in Python - Quora
Answer (1 of 2): Use the json library [1] - specifically the Json.load if the json data is in a file, or json.loads if the json data is already in a string format. the json.load (or json.loads) return a Python object which is the Python equivalent of the json data. So if for example the top le...
๐ŸŒ
LearnPython.com
learnpython.com โ€บ blog โ€บ json-in-python
How to Convert a String to JSON in Python | LearnPython.com
The dumps() method converts a Python object to a JSON formatted string. We can also create a JSON file from data stored in a Python dictionary. The method for performing this task is dump(). Letโ€™s use the dump() method for creating a JSON file.
๐ŸŒ
Real Python
realpython.com โ€บ python-json
Working With JSON Data in Python โ€“ Real Python
July 21, 2026 - The dog_data dictionary contains a bunch of common Python data types as values. For example, a string in line 2, a Boolean in line 3, a NoneType in line 7, and a tuple in line 8, just to name a few. Next, convert dog_data to a JSON-formatted string and back to Python again. Afterward, have a look at the newly created dictionary:
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ python-json-how-to-convert-a-string-to-json
Python JSON โ€“ How to Convert a String to JSON
November 9, 2021 - [ { "first_name": "Katie", "last_name": "Rodgers" }, { "first_name": "Naomi", "last_name": "Green" }, ] // or: { "employee": [ { "first_name": "Katie", "last_name": "Rodgers" }, { "first_name": "Naomi", "last_name": "Green" }, ] } //this created an 'employee' object that has 2 records. // It defines the first name and last name of an employee ยท To use JSON with Python, you'll first need to include the JSON module at the top of your Python file. This comes built-in to Python and is part of the standard library. So, say you have a file named demo.py. At the top you would add the following line: ... #include json library import json #json string data employee_string = '{"first_name": "Michael", "last_name": "Rodgers", "department": "Marketing"}' #check data type with type() method print(type(employee_string)) #output #<class 'str'>
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ gloss_python_convert_into_JSON.asp
Python Convert From Python to JSON
Remove List Duplicates Reverse ... ... If you have a Python object, you can convert it into a JSON string by using the json.dumps() method....