Python merge two json

How do I concat two jsons in python

You can load both json strings into Python Dictionaries and then combine. This will only work if there are unique keys in each json string.,To convert your string to python dictionary you use the following:,Assuming a and b are the dictionaries you want to merge:,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.

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

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 = '' jsonStringB = '' % (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 = # string dump of the merged dict jsonString_merged = json.dumps(merged_dict) 

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)) 

Answer by Linda Owen

You can load both json strings into Python Dictionaries and then combine. This will only work if there are unique keys in each json string.,To convert your string to python dictionary you use the following:,Assuming a and b are the dictionaries you want to merge:,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.

Читайте также:  Html text property value

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

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 = '' jsonStringB = '' % (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 = # string dump of the merged dict jsonString_merged = json.dumps(merged_dict) 

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)) 

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

jsonMerged = <**json.loads(jsonStringA), **json.loads(jsonStringB)>asString = json.dumps(jsonMerged) 

You can load both json strings into Python Dictionaries and then combine. This will only work if there are unique keys in each json string.

import json a = json.loads(jsonStringA) b = json.loads(jsonStringB) c = dict(a.items() + b.items()) # or c = dict(a, **b) 

Answer by Jonas Tran

In this code, we have opened the files in ‘read’ mode (which is by default) whose content we want to add in the other file.,Here, we have 3 different files of .json type so without wasting any time let’s jump into the code to see the implementation.,In both codes, we have opened file 1 and file 3 in the append mode(‘a’) respectively. Don’t you think why we didn’t use the write mode(‘w’)? If you will use write mode, it’ll replace all the existing data in the file and if you don’t want to erase the existing data you should go for append mode.,As you have seen the image on the top, we have three JSON files and the third file ‘file3.json‘ is empty now. Let’s see what will happen after the execution of code!

Without wasting time see the code given below:

f2data = "" with open('C:\\Users\\lenovo\\Documents\\file2.json') as f2: f2data = '\n' + f2.read() with open('C:\\Users\\lenovo\\Documents\\file1.json','a+') as f1: f1.write(f2data)

As you have seen the image on the top, we have three JSON files and the third file ‘file3.json‘ is empty now. Let’s see what will happen after the execution of code!

f1data = f2data = "" with open('C:\\Users\\lenovo\\Documents\\file1.json') as f1: f1data = f1.read() with open('C:\\Users\\lenovo\\Documents\\file2.json') as f2: f2data = f2.read() f1data += "\n" f1data += f2data with open ('C:\\Users\\lenovo\\Documents\\file3.json', 'a') as f3: f3.write(f1data)

Answer by Charlie Carter

I put the list in a for loop and tried json merge and I got the error head missing expected 2 arguments got 1, How to merge multiple json objects into a single. ,How to prompt for user input and read command-line arguments? Jan 5 ,How to declare an array in Python? Jan 5

Answer by Saul Peters

This Python module allows you to merge a series of JSON documents into a single one.,Merge a series of JSON documents.,The Merger class allows you to further customize the merging of JSON data by allowing you to:,Another common example is when you need to keep a versioned list of values that appeared in the series of documents:

Consider a trivial example with two documents:

We call the document we are merging changes into base and the changed document head. To merge these two documents using jsonmerge:

>>> from pprint import pprint >>> from jsonmerge import merge >>> result = merge(base, head) >>> pprint(result, width=40)

Let’s say you want to specify that the merged bar field in the example document above should contain elements from all documents, not just the latest one. You can do this with a schema like this:

>>> schema = < . "properties": < . "bar": < . "mergeStrategy": "append" . >. > . > >>> from jsonmerge import Merger >>> merger = Merger(schema) >>> result = merger.merge(base, head) >>> pprint(result, width=40)

Another common example is when you need to keep a versioned list of values that appeared in the series of documents:

>>> schema = < . "properties": < . "foo": < . "type": "object", . "mergeStrategy": "version", . "mergeOptions": < "limit": 5 >. > . >, . "additionalProperties": False . > >>> from jsonmerge import Merger >>> merger = Merger(schema) >>> rev1 = < . 'foo': < . 'greeting': 'Hello, World!' . >. > >>> rev2 = < . 'foo': < . 'greeting': 'Howdy, World!' . >. > >>> base = None >>> base = merger.merge(base, rev1, merge_options= < . 'version': < . 'metadata': < . 'revision': 1 . >. > . >) >>> base = merger.merge(base, rev2, merge_options= < . 'version': < . 'metadata': < . 'revision': 2 . >. > . >) >>> pprint(base, width=55) >, >]> 

In the following example, the property Foo (uppercase F) does not match foo (lowercase f) in the schema and hence the version strategy is not applied as with previous two revisions:

>>> rev3 = < . 'Foo': < . 'greeting': 'Howdy, World!' . >. > >>> base = merger.merge(base, rev3, merge_options= < . 'version': < . 'metadata': < . 'revision': 3 . >. > . >) >>> pprint(base, width=55) , 'foo': [>, >]> 

Hence it is recommended to validate the input documents against the schema before passing them to jsonmerge. This practice is even more effective if the schema is filled in with more information than strictly necessary for jsonmerge (e.g. adding information about types, restrict valid object properties with additionalProperties, etc.):

>>> from jsonschema import validate >>> validate(rev1, schema) >>> validate(rev2, schema) >>> validate(rev3, schema) Traceback (most recent call last): . jsonschema.exceptions.ValidationError: Additional properties are not allowed ('Foo' was unexpected) 

If you care about well-formedness of your documents, you might also want to obtain a schema for the documents that the merge method creates. jsonmerge provides a way to automatically generate it from a schema for the input document:

>>> result_schema = merger.get_schema() >>> pprint(result_schema, width=80) >>, 'maxItems': 5, 'type': 'array'>>> 

To install the latest jsonmerge release from the Python package index:

To install from source, run the following from the top of the source distribution:

jsonmerge uses Tox for testing. To run the test suite run:

Answer by Santos Drake

When to use yield instead of return in Python?,Read JSON file using Python,Read, Write and Parse JSON using Python,Working With JSON Data in Python

Answer by Araceli Harrington

Concatenate JSON arrays,Concatenate JSON arrays Example ,Plugins & Extending Dataiku DSS,This processor concatenates N input columns containing arrays (as JSON) into a single JSON array.

Answer by Emersyn McKay

We will assume that we are starting from two JSON strings and, at the end, we want to obtain a single JSON string with the merged objects. Nonetheless, we will do the merging operation over the parsed objects, on their JavaScript representation. ,We will start our code by defining two JSON strings containing two different objects. These are the objects we will merge.,After defining these JSON strings, we will parse them to JavaScript objects. We will do the merging operation over these objects and then convert the result back to a JSON string. For a more detailed tutorial on how to parse JSON strings to JavaScript objects, please check here.,After this, we will convert the merged object back to a JSON string with a call to the stringify method on the JSON object. You can check here a more detailed tutorial on how to serialize a JavaScript object to JSON.

const string1 = `< "name": "Todd", "age": 20 >`; const string2 = `< "languages": ["Spanish", "Portuguese"], "married": true >`; 

Источник

How to Merge Multiple JSON Files with Python

In this quick article, we’ll focus on a few examples of how to merge multiple JSON files into a one with Python. We will answer also on those questions:

  • How to merge 2 JSON files in Python?
  • How to merge all JSON files in directory?
  • Merge JSON files by pattern
  • Keep trace on merging multiple JSON files

If you are interested in combining CSV files into DataFrame then you can check this detailed article: How to merge multiple CSV files with Python

This image illustrates the process of merging two JSON files into single on by Python:

Merge multiple JSON files into one

To merge multiple JSON files with trace into one we can use :

import pandas as pd import glob, os, json json_dir = 'data/json_files_dir' json_pattern = os.path.join(json_dir, '*.json') file_list = glob.glob(json_pattern) dfs = [] for file in file_list: with open(file) as f: json_data = pd.json_normalize(json.loads(f.read())) json_data['site'] = file.rsplit("/", 1)[-1] dfs.append(json_data) df = pd.concat(dfs) 

If you like to learn more about this code and how to customize it. Then you can check the next steps:

Step 1: List multiple JSON files in a folder

Merging multiple files requires several Python libraries like: pandas , glob , os and json .

Next we can see how to list JSON files in a folder with Python:

import pandas as pd import glob, os, json json_dir = 'data/json_files_dir' json_pattern = os.path.join(json_dir, '*.json') file_list = glob.glob(json_pattern) 

This will result in a list with the absolute JSON files like:

Note: for JSON lines you may need to change the matching pattern to ‘*.jl’

Step 2: Read and merge multiple JSON file into DataFrame

Finally we are going to process all JSON files found in the previous step one by one.

We are reading the files with f.read() and loading them as JSON records by method json.loads .

Finally we are going to create a Pandas DataFrame with pd.json_normalize . All DataFrames are appended to a list.

The last step is concatenating list of DataFrames into a single one by: pd.concat(dfs)

dfs = [] for file in file_list: with open(file) as f: json_data = pd.json_normalize(json.loads(f.read())) json_data['site'] = file.rsplit("/", 1)[-1] dfs.append(json_data) df = pd.concat(dfs) 

If you like to have a trace of each record from which file is coming — then you can use a line like:

json_data['site'] = file.rsplit("/", 1)[-1] 

We are converting the absolute file path in the file name so:

Python merge json files — lines

Below you can find how to merge multiple JSON line files. What we need is to add parameter lines=True :

import pandas as pd import glob, os, json json_dir = '/home/user/data' json_pattern = os.path.join(json_dir, '*.json') file_list = glob.glob(json_pattern) dfs = [] for file in file_list: df_temp = pd.read_json(file, lines=True) df_temp['source'] = file.rsplit("/", 1)[-1] dfs.append(df_temp) df = pd.concat(dfs) 

Summary

In this article we covered how to merge multiple JSON files into one in Python.

We also covered merging of all JSON files in directory by pattern in Python. As a bonus we saw how to save information into Pandas DataFrame and keep track of each source JSON file.

By using SoftHints — Python, Linux, Pandas , you agree to our Cookie Policy.

Источник

Оцените статью