简体   繁体   中英

How to create a JSON file from another JSON file based on IF condition! Python3

I have a json file like this:

[
      {
            "id": "john",
            "title": "tech",
            "date": "2020/1/1",
            "shift": "DAYS"
      },
      {
            "id": "tom",
            "title": "tech",
            "date": "2021/5/5",
            "shift": "NIGHT"
      }
]

I need to re-create another JSON file based on this information. For example:

IF shift == NIGHT:

shiftHourStart = 22:00:00

shiftHourEnd = 06:00:00

So output should be like this:

[
      {
            "id": "john",
            "title": "tech",
            "shiftHourStart" = "22:00:00",
            "shiftHourEnd" = "06:00:00"
            "shift": "NIGHT"
      },
      {
            "id": "tom",
            "title": "tech",
            "date": "2021/5/5",
            "shiftHourStart" = "06:00:00",
            "shiftHourEnd" = "15:00:00",
            "shift": "DAY"          
      }
]

you can use dict.update

data = [
      {
            "id": "john",
            "title": "tech",
            "date": "2020/1/1",
            "shift": "DAYS"
      },
      {
            "id": "tom",
            "title": "tech",
            "date": "2021/5/5",
            "shift": "NIGHT"
      }
]

shift_mapping = {
    'DAYS': { # or DAY whatever you have
        "shiftHourStart": "22:00:00",
            "shiftHourEnd": "06:00:00"
    },
    'NIGHT':{
        "shiftHourStart": "06:00:00",
        "shiftHourEnd": "15:00:00",
    }
}

for each_shift in data:
    if 'shift' in each_shift:
        each_shift.update(shift_mapping[each_shift['shift']])
    else:
        print('Warning!! shift key does not exists!')
    
print(data)
[{'id': 'john',
  'title': 'tech',
  'date': '2020/1/1',
  'shift': 'DAYS',
  'shiftHourStart': '22:00:00',
  'shiftHourEnd': '06:00:00'},
 {'id': 'tom',
  'title': 'tech',
  'date': '2021/5/5',
  'shift': 'NIGHT',
  'shiftHourStart': '06:00:00',
  'shiftHourEnd': '15:00:00'}]

Use this:

import json
json_text = """[
      {
            "id": "john",
            "title": "tech",
            "date": "2020/1/1",
            "shift": "DAYS"
      },
      {
            "id": "tom",
            "title": "tech",
            "date": "2021/5/5",
            "shift": "NIGHT"
      }
]
"""
n_dict = json.loads(json_text)
if n_dict[0]['shift'] == 'DAY':
    n_dict[0]["shiftHourStart"]="06:00:00"
    n_dict[0]["shiftHourEnd"] = "15:00:00"
else:
    n_dict[0]["shiftHourStart"]="22:00:00"
    n_dict[0]["shiftHourEnd"] = "06:00:00"
new_json = json.dumps(n_dict)
print(new_json)

Edit for your case: If json file is ".\\\\file.json" then,

import json
with open('.\\file.json') as f:
  json_text = json.load(f)
n_dict = json.loads(json_text)
if n_dict[0]['shift'] == 'DAY':
    n_dict[0]["shiftHourStart"]="06:00:00"
    n_dict[0]["shiftHourEnd"] = "15:00:00"
else:
    n_dict[0]["shiftHourStart"]="22:00:00"
    n_dict[0]["shiftHourEnd"] = "06:00:00"
with open('.\\new_file.json', 'w') as json_file:
  json.dump(n_dict, json_file)

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