As of Python 3.5, you can merge two dicts with:

merged = {**dictA, **dictB}

(https://www.python.org/dev/peps/pep-0448/)

So:

jsonMerged = {**json.loads(jsonStringA), **json.loads(jsonStringB)}
asString = json.dumps(jsonMerged)

etc.

EDIT 2 Nov 2024: Pretty sure we can now do merged = dictA | dictB

Answer from P i on Stack Overflow
🌐
AskPython
askpython.com › home › what is json and how to merge two json strings?
What Is JSON and How To Merge Two Json Strings? - AskPython
March 16, 2023 - We can merge multiple json strings into one single entity. We are going to look at the following examples. ... In this example, we are going to see the usage of jsonmerge library of the PyPI repository. PyPI is the third-party repository for python packages.
Top answer
1 of 6
45

As of Python 3.5, you can merge two dicts with:

merged = {**dictA, **dictB}

(https://www.python.org/dev/peps/pep-0448/)

So:

jsonMerged = {**json.loads(jsonStringA), **json.loads(jsonStringB)}
asString = json.dumps(jsonMerged)

etc.

EDIT 2 Nov 2024: Pretty sure we can now do merged = dictA | dictB

2 of 6
39

Assuming a and b are the dictionaries you want to merge:

c = {key: value for (key, value) in (a.items() + b.items())}

To convert your string to python dictionary you use the following:

import json
my_dict = json.loads(json_str)

Update: full code using strings:

# test cases for jsonStringA and jsonStringB according to your data input
jsonStringA = '{"error_1395946244342":"valueA","error_1395952003":"valueB"}'
jsonStringB = '{"error_%d":"Error Occured on machine %s in datacenter %s on the %s of process %s"}' % (timestamp_number, host_info, local_dc, step, c)

# now we have two json STRINGS
import json
dictA = json.loads(jsonStringA)
dictB = json.loads(jsonStringB)

merged_dict = {key: value for (key, value) in (dictA.items() + dictB.items())}

# string dump of the merged dict
jsonString_merged = json.dumps(merged_dict)

But I have to say that in general what you are trying to do is not the best practice. Please read a bit on python dictionaries.


Alternative solution:

jsonStringA = get_my_value_as_string_from_somewhere()
errors_dict = json.loads(jsonStringA)

new_error_str = "Error Ocurred in datacenter %s blah for step %s blah" % (datacenter, step)
new_error_key = "error_%d" % (timestamp_number)

errors_dict[new_error_key] = new_error_str

# and if I want to export it somewhere I use the following
write_my_dict_to_a_file_as_string(json.dumps(errors_dict))

And actually you can avoid all these if you just use an array to hold all your errors.

Discussions

Combine Strings in json as single string using python - Stack Overflow
You can use str.join. This permits to join a list of strings, inserting another string in between: More on stackoverflow.com
🌐 stackoverflow.com
python - Combining two JSON objects in to one - Stack Overflow
I have two JSON objects. One is python array which is converted using json,dumps() and other contains records from database and is serialized using json serializer. I want to combine them into a si... More on stackoverflow.com
🌐 stackoverflow.com
concatenate string (variable expansion)
Why are you trying to manually construct a JSON string? You know the json module exists, right? More on reddit.com
🌐 r/learnpython
7
1
June 18, 2024
Join JSON Object member string values together
I have the above JSON parsed (using .ajax and jsonp as callback) and I would like to join all the values of "name" into a string. i.e. "Dogs, Cats, Sheep". How can I do this? More on stackoverflow.com
🌐 stackoverflow.com
🌐
W3Schools
w3schools.com › python › ref_string_join.asp
Python String join() Method
Remove List Duplicates Reverse ... Bootcamp Python Certificate Python Training ... The join() method takes all items in an iterable and joins them into one string....
🌐
W3Schools
w3schools.com › python › python_json.asp
Python JSON
Python Strings Slicing Strings Modify Strings Concatenate Strings Format Strings Escape Characters String Methods String Exercises Code Challenge Python Booleans ... Python Operators Arithmetic Operators Assignment Operators Comparison Operators Logical Operators Identity Operators Membership Operators Bitwise Operators Operator Precedence Code Challenge Python Lists · Python Lists Access List Items Change List Items Add List Items Remove List Items Loop Lists List Comprehension Sort Lists Copy Lists Join Lists List Methods List Exercises Code Challenge Python Tuples
🌐
PyPI
pypi.org › project › jsonmerge
jsonmerge · PyPI
Merge a series of JSON documents. ... This Python module allows you to merge a series of JSON documents into a single one.
      » pip install jsonmerge
    
Published   Jul 19, 2023
Version   1.9.2
🌐
Real Python
realpython.com › python-join-string
How to Join Strings in Python – Real Python
January 20, 2025 - In this tutorial, you'll learn how to use Python's built-in .join() method to combine string elements from an iterable into a single string with a specified separator. You'll also learn about common pitfalls, and how CPython makes .join() work efficiently.
Find elsewhere
🌐
Bobby Hadz
bobbyhadz.com › blog › merge-two-json-objects-in-python
How to merge two JSON objects in Python [5 Ways] | bobbyhadz
April 11, 2024 - Copied!import json obj1 = json.dumps({'id': 1, 'name': 'bobby hadz'}) obj2 = json.dumps({'site': 'bobbyhadz.com', 'topic': 'Python'}) dict1 = json.loads(obj1) dict2 = json.loads(obj2) merged_dict = { key: value for (key, value) in list(dict1.items()) + list(dict2.items()) } # 👇️ {'id': 1, 'name': 'bobby hadz', 'site': 'bobbyhadz.com', 'topic': 'Python'} print(merged_dict) ... Dict comprehensions are very similar to list comprehensions. They perform some operation for every key-value pair in the dictionary or select a subset of key-value pairs that meet a condition. If you need to exclude
🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-merge-multiple-json-files-using-python
How to Merge Multiple JSON Files Using Python - GeeksforGeeks
July 23, 2025 - Below are the two JSON files that we will use in our article to merge them into a single JSON file. ... In this example, a Python function merge_json_files is defined to combine data from multiple JSON files specified by file_paths into a list called merged_data.
🌐
Medium
medium.com › @programinbasic › merge-multiple-json-files-into-one-in-python-65c009aad81d
Merge Multiple JSON files into One in Python | by ProgrammingBasic | Medium
January 17, 2024 - So this is how you can merge JSON files into one single file using Pythons’s built-in json modules or using Pandas library.
🌐
Reddit
reddit.com › r/learnpython › concatenate string (variable expansion)
r/learnpython on Reddit: concatenate string (variable expansion)
June 18, 2024 -

Hallo Team,

i did gave my best but looks like I'm getting through.

How I do added the string inside json section

Basically, I need to create a token, catch that in variable and added that variable inside json section

headers = {
  'Authorization': 'Bearer xyz'
}

basically xyz is something i should replace with variable.

how to make it work?

Above is not working for some reason.

# Check request

import requests
import urllib3
import json

# define variables
sddcmfqdn = "sddc-md-vcf0-ko.zonkos.ict"

api_endpoint = "https://" + sddcmfqdn # constant
api_version = "/v1/" # constant
bearer = " '" + "Bearer " # constant

api_subject = "domains"
accessToken = "eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiJkMzNmZmUyNy0yOTBiLTRlMjgtYWNlOS0yYzUxYTE5NGYxMDMiLCJpYXQiOjE3MTg2OTY4OTYsInN1YiI6ImFkbWluaXN0cmF0b3JAd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Q1JFREVOVElBTF9SRUFEIiwiSE9TVF9SRUFEIiwiQ0VJUF9XUklURSIsIlJFU09VUkNFX0xPQ0tfUkVBRCIsIk9USEVSX1dSSVRFIiwiTElDRU5TRV9LRVlfV1JJVEUiLCJSRVNPVVJDRV9GVU5DVElPTkFMSVRZX1JFQUQiLCJDQV9SRUFEIiwiVEFHX1JFQUQiLCJMSUNFTlNJTkdfUkVBRCIsIk5FVFdPUktfUE9PTF9XUklURSIsIldDUF9SRUFEIiwiTElDRU5TSU5HX0lORk9fV1JJVEUiLCJCQUNLVVBfUkVTVE9SRV9XUklURSIsIk5UUF9SRUFEIiwiRURHRV9DTFVTVEVSX1JFQUQiLCJFVkVOVF9SRUFEIiwiQkFDS1VQX0NPTkZJR19XUklURSIsIldDUF9XUklURSIsIlNFUlZJQ0VfQUNDT1VOVF9XUklURSIsIk5FVFdPUktfUE9PTF9SRUFEIiwiQ0FfV1JJVEUiLCJDTFVTVEVSX1JFQUQiLCJWQVNBX1BST1ZJREVSX1dSSVRFIiwiRE5TX1dSSVRFIiwiU1lTVEVNX1dSSVRFIiwiVlJTTENNX1dSSVRFIiwiRE5TX1JFQUQiLCJTRVJWSUNFX0FDQ09VTlRfUkVBRCIsIlNERENfRkVERVJBVElPTl9SRUFEIiwiRE9NQUlOX1JFQUQiLCJWUlNMQ01fUkVBRCIsIlVQR1JBREVfV1JJVEUiXSwicm9sZSI6WyJBRE1JTiJdfQ.sPhAkoCQdjLworedqJgntkq9_DqvAahA2XhAy2QZbZo"
bearer_token = bearer + accessToken + "'"

# build api url
api_url = api_endpoint + api_version + api_subject
print(api_url)

test1 = "'" + 'Authorization' + "'" + ":"
test2 = bearer_token

test3 = test1 + test2
print(test3)

# payload
payload = {}
headers = {
    test3
}

# response = requests.request("GET", api_url, headers=headers, data=payload, verify=False)
# print(response.text)

any help? or better way to achieve this?

Top answer
1 of 2
3
Why are you trying to manually construct a JSON string? You know the json module exists, right?
2 of 2
1
Found the solution. I was adding quote... # Check request import requests import urllib3 import json # define variables sddcmfqdn = "sddc-md-vcf0-ko.zonkos.ict" api_endpoint = "https://" + sddcmfqdn # constant api_version = "/v1/" # constant bearer = " '" + "Bearer " # constant api_subject = "domains" accessToken = "eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiJkMzNmZmUyNy0yOTBiLTRlMjgtYWNlOS0yYzUxYTE5NGYxMDMiLCJpYXQiOjE3MTg2OTY4OTYsInN1YiI6ImFkbWluaXN0cmF0b3JAdnNwaGVyZS5sb2NhbCIsImlzcyI6InZjZi1hdXRoIiwiYXVkIjoic2RkYy1zZXJ2aWNlcyIsIm5iZiI6MTcxODY5Njg5NiwiZXhwIjoxNzE4NzAwNDk2LCJ1c2VyIjoiYWRtaW5pc3RyYXRvckB2c3BoZXJlLmxvY2FsIiwibmFtZSI6ImFkbWluaXN0cmF0b3JAdnNwaGVyZS5sb2NhbCIsInNjb3BlIjpbIlJFU09VUkNFX0ZVTkNUSU9OQUxJVFlfV1JJVEUiLCJMSUNFTlNJTkdfSU5GT19SRUFEIiwiU0REQ19GRURFUkFUSU9OX1dSSVRFIiwiQVZOX1dSSVRFIiwiU0REQ19NQU5BR0VSX1JFQUQiLCJDRVJUX1dSSVRFIiwiQ09NUE9TQUJJTElUWV9XUklURSIsIkxJQ0VOU0VfS0VZX1JFQUQiLCJDT01QT1NBQklMSVRZX1JFQUQiLCJFREdFX0NMVVNURVJfV1JJVEUiLCJVU0VSX1JFQUQiLCJDUkVERU5USUFMX1dSSVRFIiwiQkFDS1VQX0NPTkZJR19SRUFEIiwiQ0xVU1RFUl9XUklURSIsIkFWTl9SRUFEIiwiVkFTQV9QUk9WSURFUl9SRUFEIiwiRE9NQUlOX1dSSVRFIiwiQ0VJUF9SRUFEIiwiU09TX1dSSVRFIiwiU0REQ19NQU5BR0VSX1dSSVRFIiwiTlRQX1dSSVRFIiwiVEFHX1dSSVRFIiwiREVQT1RfQ09ORklHX1dSSVRFIiwiU1lTVEVNX1JFQUQiLCJERVBPVF9DT05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api url api_url = api_endpoint + api_version + api_subject print(api_url) # payload payload = {} headers = { 'Authorization' : 'Bearer ' + accessToken } response = requests.request("GET", api_url, headers=headers, data=payload, verify=False) print(response.text)
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-ways-to-convert-string-to-json-object
Convert String to JSON Object - Python - GeeksforGeeks
json.loads() method is the most commonly used function for parsing a JSON string and converting it into a Python dictionary. The method takes a string containing JSON data and deserializes it into a Python object.
Published   July 11, 2025
🌐
Like Geeks
likegeeks.com › home › python › 8+ examples for merging json arrays in python
8+ Examples for Merging JSON arrays in Python
February 8, 2024 - Learn to merge JSON arrays in Python with this tutorial. Discover techniques using + operator, reduce(), and jsonmerge for efficient data handling.
🌐
Reddit
reddit.com › r/learnpython › what is the best way to merge two json file in python?
r/learnpython on Reddit: What is the best way to merge two JSON file in Python?
May 2, 2024 -

Hello everyone,

I am trying to merge two JSON files, but I couldn't find any quick package that can do this. One file contains the base policy, while the other includes additional files for excluding special configurations.

My goal is to merge these two JSON files of AntiVirus policy, which contain arrays and numerous elements, without overwriting any data. I was wondering what the best approach would be to accomplish this.

If its element just uses the value of the other files.
If its array just append new elements.

What is best way to achieve this goal?

Thanks all

🌐
Real Python
realpython.com › python-json
Working With JSON Data in Python – Real Python
August 20, 2025 - Learn how to work with JSON data in Python using the json module. Convert, read, write, and validate JSON files and handle JSON data for APIs and storage.