Join 2 JSON inputs linked by Primary Key

后端 未结 1 556
无人及你
无人及你 2021-01-23 07:23

I am trying to merge 2 JSON inputs (this example is from a file, but it will be from a Google Pub Sub input later) from these:

orderID.json:    
{\"orderID\":\"t         


        
相关标签:
1条回答
  • 2021-01-23 07:36

    Try the below, Hope this will help you a little.

    Here i have used an array of your order and combined, instead of using a file.

    order = [{"orderID":"test1","orderPacked":"Yes","orderSubmitted":"Yes","orderVerified":"Yes","stage":1}]
    
    combined = [
       {"barcode":"95590","name":"Ash","quantity":6,"orderID":"test1"},
       {"barcode":"95591","name":"Beat","quantity":6,"orderID":"test1"},
       {"barcode":"95592","name":"Cat","quantity":6,"orderID":"test1"},
       {"barcode":"95593","name":"Dog","quantity":6,"orderID":"test2"},
       {"barcode":"95594","name":"Scar","quantity":6,"orderID":"test2"}
       ]
    
    
    def joinjson(repl, tobeCombined):
      newarr = []
      for data in tobeCombined:
        replData = getOrderData(repl,data['orderID'])
        if replData is not None:
          data.update(replData)
        newarr.append(data)
    
      return newarr
    
    def getOrderData(order, orderID):
      for data in order:
        print("Data OrderID : ",data['orderID'])
        if data['orderID'] == orderID:
          return data
    
    
    
    print(joinjson(order,combined))
    
    0 讨论(0)
提交回复
热议问题