Home > OS >  python complete json objects in file from another json file
python complete json objects in file from another json file

Time:03-15

I have two json file: file1.json and file2.json successively created. I would like to complete file1 by adding the data of file2 for each object. Objects are exactly of the same number and have all same attributes

File 1 and 2 have strictly identical structure : lists in file1 (open, close, ...) will be appended with values in file2, attributes like deltasum, detlatot would be changed with the values in file2.

[{"symbol": "AUDUSD=X", "open": [0.72, 0.72], "close": [0.72, 0.72], "high": [0.72, 0.73], "low": [0.72, 0.73], "timestamp": [1647262622, 1647262682], "volume": [0, 0], "delta": [0.0, 0.0], "deltasum": 2.839999999999999, "deltatot": 2.8022723967505714, "voltot": 0, "cmfs": [0, 0, 0, 0], "mfms": [0, -1.0, 0, 0, 0], "resist": [0, 0, 0]}, {"symbol": "AUDNZD=X", …

I have tried dumps style but I can't rebuild the file as I want. Thanks a lot guys for your support or ideas.

CodePudding user response:

I assumes you have the same symbols in both lists, and symbols are in the same order on list.

You can use for-loop with zip() to work with rows from first and second as pair.

And later you can use .extend or = to add all values from one list to another.

a = [
    {"symbol": "AUDUSD=X", "open": [0.72, 0.72], "close": [0.72, 0.72], "high": [0.72, 0.73], "low": [0.72, 0.73], "timestamp": [1647262622, 1647262682], "volume": [0, 0], "delta": [0.0, 0.0], "deltasum": 2.839999999999999, "deltatot": 2.8022723967505714, "voltot": 0, "cmfs": [0, 0, 0, 0], "mfms": [0, -1.0, 0, 0, 0], "resist": [0, 0, 0]},
    {"symbol": "AUDNZD=X", "open": [0.72, 0.72], "close": [0.72, 0.72], "high": [0.72, 0.73], "low": [0.72, 0.73], "timestamp": [1647262622, 1647262682], "volume": [0, 0], "delta": [0.0, 0.0], "deltasum": 2.839999999999999, "deltatot": 2.8022723967505714, "voltot": 0, "cmfs": [0, 0, 0, 0], "mfms": [0, -1.0, 0, 0, 0], "resist": [0, 0, 0]},
]

b = [
    {"symbol": "AUDUSD=X", "open": [0.72, 0.72], "close": [0.72, 0.72], "high": [0.72, 0.73], "low": [0.72, 0.73], "timestamp": [1647262622, 1647262682], "volume": [0, 0], "delta": [0.0, 0.0], "deltasum": 2.839999999999999, "deltatot": 2.8022723967505714, "voltot": 0, "cmfs": [0, 0, 0, 0], "mfms": [0, -1.0, 0, 0, 0], "resist": [0, 0, 0]},
    {"symbol": "AUDNZD=X", "open": [0.72, 0.72], "close": [0.72, 0.72], "high": [0.72, 0.73], "low": [0.72, 0.73], "timestamp": [1647262622, 1647262682], "volume": [0, 0], "delta": [0.0, 0.0], "deltasum": 2.839999999999999, "deltatot": 2.8022723967505714, "voltot": 0, "cmfs": [0, 0, 0, 0], "mfms": [0, -1.0, 0, 0, 0], "resist": [0, 0, 0]},
]

for a_row, b_row in zip(a, b):
    
    # extend lists
    #a_row['open'].extend( b_row['open'] )
    a_row['open']   = b_row['open']
    a_row['close']  = b_row['close']
    a_row['high']   = b_row['high']
    a_row['low']    = b_row['low']
    a_row['timestamp']  = b_row['timestamp']
    a_row['volume']     = b_row['volume']
    # ... etc.
    
    # replace values
    a_row['deltasum'] = b_row['deltasum']
    a_row['deltatot'] = b_row['deltatot']
    # ... etc.
    
import pprint
pprint.pprint(a)

You can make it shorter if you use inner for-loops

a = [
    {"symbol": "AUDUSD=X", "open": [0.72, 0.72], "close": [0.72, 0.72], "high": [0.72, 0.73], "low": [0.72, 0.73], "timestamp": [1647262622, 1647262682], "volume": [0, 0], "delta": [0.0, 0.0], "deltasum": 2.839999999999999, "deltatot": 2.8022723967505714, "voltot": 0, "cmfs": [0, 0, 0, 0], "mfms": [0, -1.0, 0, 0, 0], "resist": [0, 0, 0]},
    {"symbol": "AUDNZD=X", "open": [0.72, 0.72], "close": [0.72, 0.72], "high": [0.72, 0.73], "low": [0.72, 0.73], "timestamp": [1647262622, 1647262682], "volume": [0, 0], "delta": [0.0, 0.0], "deltasum": 2.839999999999999, "deltatot": 2.8022723967505714, "voltot": 0, "cmfs": [0, 0, 0, 0], "mfms": [0, -1.0, 0, 0, 0], "resist": [0, 0, 0]},
]

b = [
    {"symbol": "AUDUSD=X", "open": [0.72, 0.72], "close": [0.72, 0.72], "high": [0.72, 0.73], "low": [0.72, 0.73], "timestamp": [1647262622, 1647262682], "volume": [0, 0], "delta": [0.0, 0.0], "deltasum": 2.839999999999999, "deltatot": 2.8022723967505714, "voltot": 0, "cmfs": [0, 0, 0, 0], "mfms": [0, -1.0, 0, 0, 0], "resist": [0, 0, 0]},
    {"symbol": "AUDNZD=X", "open": [0.72, 0.72], "close": [0.72, 0.72], "high": [0.72, 0.73], "low": [0.72, 0.73], "timestamp": [1647262622, 1647262682], "volume": [0, 0], "delta": [0.0, 0.0], "deltasum": 2.839999999999999, "deltatot": 2.8022723967505714, "voltot": 0, "cmfs": [0, 0, 0, 0], "mfms": [0, -1.0, 0, 0, 0], "resist": [0, 0, 0]},
]

for a_row, b_row in zip(a, b):
    
    # extend lists
    for key in ['open', 'close', 'high', 'low', 'timestamp', 'volume']:  # ... etc. ...
        #a_row[key].extend( b_row[key] )
        a_row[key]  = b_row[key]
    
    # replace values
    for key in ['deltasum', 'deltatot']: # ... etc. ...
        a_row[key] = b_row[key]
    
import pprint
pprint.pprint(a)

You can also get every key,value from row and use isinstence() to check if value is list or string - and run different function

a = [
    {"symbol": "AUDUSD=X", "open": [0.72, 0.72], "close": [0.72, 0.72], "high": [0.72, 0.73], "low": [0.72, 0.73], "timestamp": [1647262622, 1647262682], "volume": [0, 0], "delta": [0.0, 0.0], "deltasum": 2.839999999999999, "deltatot": 2.8022723967505714, "voltot": 0, "cmfs": [0, 0, 0, 0], "mfms": [0, -1.0, 0, 0, 0], "resist": [0, 0, 0]},
    {"symbol": "AUDNZD=X", "open": [0.72, 0.72], "close": [0.72, 0.72], "high": [0.72, 0.73], "low": [0.72, 0.73], "timestamp": [1647262622, 1647262682], "volume": [0, 0], "delta": [0.0, 0.0], "deltasum": 2.839999999999999, "deltatot": 2.8022723967505714, "voltot": 0, "cmfs": [0, 0, 0, 0], "mfms": [0, -1.0, 0, 0, 0], "resist": [0, 0, 0]},
]

b = [
    {"symbol": "AUDUSD=X", "open": [0.72, 0.72], "close": [0.72, 0.72], "high": [0.72, 0.73], "low": [0.72, 0.73], "timestamp": [1647262622, 1647262682], "volume": [0, 0], "delta": [0.0, 0.0], "deltasum": 2.839999999999999, "deltatot": 2.8022723967505714, "voltot": 0, "cmfs": [0, 0, 0, 0], "mfms": [0, -1.0, 0, 0, 0], "resist": [0, 0, 0]},
    {"symbol": "AUDNZD=X", "open": [0.72, 0.72], "close": [0.72, 0.72], "high": [0.72, 0.73], "low": [0.72, 0.73], "timestamp": [1647262622, 1647262682], "volume": [0, 0], "delta": [0.0, 0.0], "deltasum": 2.839999999999999, "deltatot": 2.8022723967505714, "voltot": 0, "cmfs": [0, 0, 0, 0], "mfms": [0, -1.0, 0, 0, 0], "resist": [0, 0, 0]},
]

for a_row, b_row in zip(a, b):
    
    for key, value in b_row.items():
        if isinstance(value, list):
            # extend lists
            a_row[key]  = value # b_row[key]
        else:
            # replace values
            a_row[key] = value # b_row[key]
    
import pprint
pprint.pprint(a)
  • Related