0

I am new to JSON, never used python packages to manipulate JSON files. I have 10 JSON files that I would like to merge into one using python.

Each of 10 files has the exact same structure and has about 50,000 entries

Example:

File one

{"tracking_code":"21703238","from_country":"FR","to_country":"FR","amount":3.23}
...

Example: File two

{"tracking_code":"41545695","from_country":"FR","to_country":"FR","amount":2.9}
...

Desired output would simply be:

{"tracking_code":"21703238","from_country":"FR","to_country":"FR","amount":3.23}
{"tracking_code":"41545695","from_country":"FR","to_country":"FR","amount":2.9}

The second part of my questions would be this - how would I join JSON files based on one key? I would like to join these 2 files by "tracking_code", the output file would simply add '"amount":3.23' to the first file.

Example: File one:

{"tracking_code":"29285908","from_country":"FR","to_country":"FR",
"package_type_id":10,"transaction_id":172238850,
"shipping_label_created":"2018-09-25 18:40:52"}

Example: File two

{"tracking_code":"29285908","from_country":"FR","to_country":"FR","amount":3.23}

Desired output:

{"tracking_code":"29285908","from_country":"FR","to_country":"FR",
"package_type_id":10,"transaction_id":172238850,
"shipping_label_created":"2018-09-25 18:40:52","amount":3.23}

Thank you.

1

1 Answer 1

0

If you use json.loads() (that "converts" json to python dictionary), you can merge them using a similar function :

def dict_merge(dict1, dict2): 
    return(dict2.update(dict1)) 

and then use json.dumps() to serialize the resulting dictionary to json.

Other solution :

you can also use json-merger (installation via pip install json-merger)

Sign up to request clarification or add additional context in comments.

3 Comments

How would I go if I have 10 files? Function with 10 inputs would look ugly, I would assume loop over files?
indeed, you would have to loop over files

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.