0

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)
Zoe
  • 27,060
  • 21
  • 118
  • 148
damu
  • 1
  • 2
  • the first one and the expected output is `ndjson`? Is this really the case? – buran Jul 18 '21 at 08:27
  • trying to add timestamps from file2 to file1 . – damu Jul 18 '21 at 08:30
  • 1
    _its a normal json file._ - This is not what you show - your snippets are not valid `json`. What you show is `ndjson`. Each line is valid `json`, but not the whole file. – buran Jul 18 '21 at 08:38
  • 1
    First step is to read the data and to verify that works correctly. Then, once you have it in memory, create a third piece of data, which you fill with the results of processing the input data. Then, you write the results to the output file. Approach each step separately, to make your task easier. Also, if you encounter problems on your overall project, it allows you to go back to one of the separate steps, adjust them and solve the problem in isolation there. – Ulrich Eckhardt Jul 18 '21 at 11:17

2 Answers2

0

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.

Hemera
  • 55
  • 1
  • 9
0

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)
Edo Akse
  • 4,051
  • 2
  • 10
  • 21