简体   繁体   中英

Merging 2 json files

I'm trying to merge both json files but I'm trying to append timestamp from file2 to corresponding frame number in file1.please guide.

JSON_FILE1

{"frameNumber":1,"classifications":[],"objects":[{"featureId":"ckotybs4v00033b68edh8a6o5","schemaId":"ckoto8fzm16gj0y7uesrd0nzt","title":"Person 1","value":"person_1","color":"#1CE6FF","keyframe":true,"bbox":{"top":157,"left":581,"height":390,"width":297},"classifications":[]}]}
{"frameNumber":2,"classifications":[],"objects":[{"featureId":"ckotybs4v00033b68edh8a6o5","schemaId":"ckoto8fzm16gj0y7uesrd0nzt","title":"Person 1","value":"person_1","color":"#1CE6FF","keyframe":false,"bbox":{"top":157,"left":581,"height":390.36,"width":297.16},"classifications":[]}]}
{"frameNumber":3,"classifications":[],"objects":[{"featureId":"ckotybs4v00033b68edh8a6o5","schemaId":"ckoto8fzm16gj0y7uesrd0nzt","title":"Person 1","value":"person_1","color":"#1CE6FF","keyframe":false,"bbox":{"top":157,"left":581,"height":390.72,"width":297.32},"classifications":[]}]}
{"frameNumber":4,"classifications":[],"objects":[{"featureId":"ckotybs4v00033b68edh8a6o5","schemaId":"ckoto8fzm16gj0y7uesrd0nzt","title":"Person 1","value":"person_1","color":"#1CE6FF","keyframe":false,"bbox":{"top":157,"left":581,"height":391.08,"width":297.48},"classifications":[]}]}
{"frameNumber":5,"classifications":[],"objects":[{"featureId":"ckotybs4v00033b68edh8a6o5","schemaId":"ckoto8fzm16gj0y7uesrd0nzt","title":"Person 1","value":"person_1","color":"#1CE6FF","keyframe":false,"bbox":{"top":157,"left":581,"height":391.44,"width":297.64},"classifications":[]}]}

JSON_FILE2

{
    "frame1": "0:0:0:66",
    "frame2": "0:0:0:100",
    "frame3": "0:0:0:133",
    "frame4": "0:0:0:166",
    "frame5": "0:0:0:200"
}

expected output:

{"frameNumber":1,"frame1": "0:0:0:66",,"classifications":[],"objects":[{"featureId":"ckotybs4v00033b68edh8a6o5","schemaId":"ckoto8fzm16gj0y7uesrd0nzt","title":"Person 1","value":"person_1","color":"#1CE6FF","keyframe":true,"bbox":{"top":157,"left":581,"height":390,"width":297},"classifications":[]}]}
{"frameNumber":2, "frame2": "0:0:0:10,"classifications":[],"objects":[{"featureId":"ckotybs4v00033b68edh8a6o5","schemaId":"ckoto8fzm16gj0y7uesrd0nzt","title":"Person 1","value":"person_1","color":"#1CE6FF","keyframe":false,"bbox":{"top":157,"left":581,"height":390.36,"width":297.16},"classifications":[]}]}
{"frameNumber":3,"frame3": "0:0:0:133,"classifications":[],"objects":[{"featureId":"ckotybs4v00033b68edh8a6o5","schemaId":"ckoto8fzm16gj0y7uesrd0nzt","title":"Person 1","value":"person_1","color":"#1CE6FF","keyframe":false,"bbox":{"top":157,"left":581,"height":390.72,"width":297.32},"classifications":[]}]}
{"frameNumber":4,"frame4": "0:0:0:166","classifications":[],"objects":[{"featureId":"ckotybs4v00033b68edh8a6o5","schemaId":"ckoto8fzm16gj0y7uesrd0nzt","title":"Person 1","value":"person_1","color":"#1CE6FF","keyframe":false,"bbox":{"top":157,"left":581,"height":391.08,"width":297.48},"classifications":[]}]}
{"frameNumber":5,"frame5": "0:0:0:200","classifications":[],"objects":[{"featureId":"ckotybs4v00033b68edh8a6o5","schemaId":"ckoto8fzm16gj0y7uesrd0nzt","title":"Person 1","value":"person_1","color":"#1CE6FF","keyframe":false,"bbox":{"top":157,"left":581,"height":391.44,"width":297.64},"classification

I tried this way but I am unable to achieve.

import json
import glob

result = []
for f in glob.glob("*.json"):
    with open(f,"rb") as infile:
        result.append(json.load(infile))

with open("merged_file.json","wb") as outfile:
    json.dump(result,outfile)

A correct .json needs a pair of [] and than you could json.load it, iterate over ever line and do the same like below but anyway: The easiest solution is turn every line in a dict , if the framenumber matches add the timestamp and write it back.

    def fuse(file1, file2, nTargetPath):
        with open(nTargetPath, "wb") as tTargetFile:
            with open(file1, "rb") as tSourceFileA:
                for tLineA in tSourceFileA.readlines():
                    tDictA = json.loads(tLineA) #loads dict from a string
                    tKey = "frame"+tDictA["frameNumber"] #searching the correct entry but why not name this timestampX
                    with open(file2, "rb") as tSourceFileB:
                        for tLineB in tSourceFileB.readlines():
                            tDictB = json.loads(tLineB )
                            if tKey in tDictB:
                                tDictA[tKey] = tDictB[tKey]
                                break #cause there is only one timestamp
                    tTargetFile.write(json.dumps(tDictA)+'\n')

This code cann easily updated by improve the file accessing for example when you know the key for the timestamp in file2 is everytime in the same row as in file1 and so on.

As was pointed out, one file is ndjson and the other file is json . You need to implement some logic to add the json to the ndjson

# https://pypi.org/project/ndjson/
# pip install ndjson
import ndjson
import json


with open('path/to/file/im_a_ndjson.ndjson') as infile:
    ndjson_object = ndjson.load(infile)


with open('path/to/file/json_file2.json') as infile:
    dict_object = json.load(infile)


print(type(ndjson_object[0]['frameNumber']))
# output: <class 'int'>


for key in dict_object:
    # int needed as you can see above
    framenumber = int(key.strip('frame'))
    # find the matching ndjson object
    for ndjs in ndjson_object:
        if ndjs['frameNumber'] == framenumber:
            # add the key/value pair
            ndjs[key] = dict_object[key]
            # we can break as we've found it
            break


with open('path/to/file/new_ndjson.ndjson', 'w') as outfile:
    ndjson.dump(ndjson_object, outfile)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM