0
  1. Load JSON file to aPool list at beginning.

    def LoadPoolTemplate():
      sFilename = './pool_template.json'
    
      if os.path.exists(sFilename):
        with open(sFilename, 'r') as fJsonFile:
          aPool = json.load(fJsonFile)
    
      return aPool
    
  2. Append aTempJson to aPool[index]['devices']

    def UpdateJSON(aPool, aDevList):
      sFilename = './device_template.json'
      if os.path.exists(sFilename):
        with open(sFilename, 'r') as fJsonFile:
          aTempJson = json.load(fJsonFile)
      else:
        print ("No such file names " + sFilename)
    
      for item in aDevList:
        aTempJson['id'] = item[1]
        aTempJson['atti']['high'] = item[2]
    
        for (i,pid) in enumerate(aPool):
          if pid['id'] == item[0]:
            aPool[i]['devices'].append(aTempJson)
            break
    
  3. Update aPool list to JSON file

    def CreateDeviceJSON(aDevice):
      with open(gDevice, 'w') as fOutfile:
        json.dump(aDevice, fOutfile, indent=2)
    
  4. Read list

    def ReadDeviceList():
      aDevList = []
    
      with open(gDevList, 'r') as fList:
        for line in fList:
          columns = line.strip().split(',')
          aDevList.append(columns)
    
      return aDevList
    
  5. main function

    def main():
      aDevDist = []
      aDeviceJson = []
    
      aDeviceJson = LoadPoolTemplate()
      aDeviceList = ReadDeviceList()
      aDeviceJson = UpdateJSON(aDeviceJson, aDeviceList)
    
      CreateDeviceJSON(aDeviceJson)
    

I don't know why all the elements in devices list are the same, please help me.

JSON file output:

[
  {
    "id": "id1",
    "devices": [
      {
         "atti": {
          "high": "190",
          "weight": "80"
        },
        "id": "Jordan"
      },
      {
         "atti": {
          "high": "190",
          "weight": "80"
        },
        "id": "Jordan"
      },
      {
         "atti": {
          "high": "190",
          "weight": "80"
        },
        "id": "Jordan"
      }
    ]
  },
  {
    "id": "id2",
    "devices": [
      {
         "atti": {
          "high": "190",
          "weight": "80"
        },
        "id": "Jordan"
      }
    ]
  },
  {
    "id": "id3",
    "devices": [
      {
         "atti": {
          "high": "190",
          "weight": "80"
        },
        "id": "Jordan"
      }
    ]
  }
]

Input source as following:

["id1", "apple", "167"]
["id1", "carter", "203"]
["id1", "jason", "188"]
["id2", "paul", "178"]
["id3", "Jordan", "190"]

Pool template

[
  {
    "id": "id1",
    "devices": [
    ]
  },
  {
    "id": "id2",
    "devices": [
    ]
  },
  {
    "id": "id3",
    "devices": [
    ]
  }
]
M.B
  • 1
  • 1
  • What is the `pool_template.json` file? I also think you need to add some "driver" code that calls the functions and reproduces the problem described. – martineau Dec 07 '18 at 11:36
  • @martineau I already update main function and pool_template.json. Please help me to take a look. thanks a lot. – M.B Dec 07 '18 at 12:57
  • use deepcopy to copy a list then fix it. – M.B Dec 12 '18 at 09:39
  • Yes: Make a copy, apply fixes to the copy, then append the copy with fixes to the list. – martineau Dec 12 '18 at 09:52

1 Answers1

0

Here:

for item in aDevList:
    aTempJson['id'] = item[1]
    aTempJson['atti']['high'] = item[2]

    for (i,pid) in enumerate(aPool):
      if pid['id'] == item[0]:
        aPool[i]['devices'].append(aTempJson)
        break

you are modifying the same aTempJson dict and appending it to the list over and over. What you have to do is to create a new dict for each item.

bruno desthuilliers
  • 75,974
  • 6
  • 88
  • 118
  • I think I always update aTempJson dict again and again, so append it to the end of list. Is that right? update contant aTempJson['id'] = item[1], aTempJson['atti']['high'] = item[2] – M.B Dec 07 '18 at 12:44