jsonformatindentation

How to preserve json file format when writing a file


I have two json files data1.json and data2.json. I compare two files and output the differences found in data1.json to a new file data3.json. I would like to preserve the format of data3.json as data1.json or data2.json.

data1.json:

    [
        {
            "userid": "1290126777",
            "status": "UserStatus.RECENTLY",
            "name": "Apple Plus",
            "bot": false,
            "username": "None"
        },
        {
            "userid": "1005441066",
            "status": "UserStatus.RECENTLY",
            "name": "SNGOUN",
            "bot": false,
            "username": "Dr_PHEAKDEY_Abdominal"
        },
        {
            "userid": "6590307444",
            "status": "UserStatus.RECENTLY",
            "name": "Amisha",
            "bot": false,
            "username": "Amishawenda"
        },
        {
            "userid": "5474490111",
            "status": "UserStatus.RECENTLY",
            "name": "Ah Nin",
            "bot": false,
            "username": "None"
        }
    ]


    data2.json:

    [
        {
            "userid": "1290126990",
            "status": "UserStatus.RECENTLY",
            "name": "Apple Plus",
            "bot": false,
            "username": "None"
        },
        {
            "userid": "1005441066",
            "status": "UserStatus.RECENTLY",
            "name": "SNGOUN",
            "bot": false,
            "username": "Dr_PHEAKDEY_Abdominal"
        },
        {
            "userid": "6590307468",
            "status": "UserStatus.RECENTLY",
            "name": "Amisha",
            "bot": false,
            "username": "Amishawenda"
        },
        {
            "userid": "5474490329",
            "status": "UserStatus.RECENTLY",
            "name": "Ah Nin",
            "bot": false,
            "username": "None"
        }
    ]


    Here are my codes:

    import json
    with open("data1.json", "r", encoding='utf-8') as f1:
        data1 = json.loads(f1.read())
    with open("data2.json", "r", encoding='utf-8') as f2:
        data2 = json.loads(f2.read())

    with open('data3.json', 'w', encoding='utf-8') as nf:
      nf.write('[' + '\n')
      for item in data1:
        if item['userid'] not in [x['userid'] for x in data2]:
          json.dump(item, nf, ensure_ascii=False, indent=4)
          nf.write(',' + '\n')
      nf.write(']')
      

    Here are the results in data3.json:

    [
    {
        "userid": "1290126777",
        "status": "UserStatus.RECENTLY",
        "name": "Apple Plus",
        "bot": false,
        "username": "None"
    },
    {
        "userid": "6590307444",
        "status": "UserStatus.RECENTLY",
        "name": "Amisha",
        "bot": false,
        "username": "Amishawenda"
    },
    {
        "userid": "5474490111",
        "status": "UserStatus.RECENTLY",
        "name": "Ah Nin",
        "bot": false,
        "username": "None"
    },
    ]   

I would like to have the same format of data3.json as data1.json or data2.json which should be:

-There is "[" at the beginning and "]" at the end

-Indentation in front of each item

-Without "," at for the last item

    The format of data3.json should be like:

    [
            {
                "userid": "1290126777",
                "status": "UserStatus.RECENTLY",
                "name": "Apple Plus",
                "bot": false,
                "username": "None"
            },
            {
                "userid": "6590307444",
                "status": "UserStatus.RECENTLY",
                "name": "Amisha",
                "bot": false,
                "username": "Amishawenda"
            },
            {
                "userid": "5474490111",
                "status": "UserStatus.RECENTLY",
                "name": "Ah Nin",
                "bot": false,
                "username": "None"
            }
    ]

Is there any way of achieving the above.

Thanks


Solution

  • It is better to build a list of required items and dump it to file in json format. Something like:

    import json
        
    with open("data1.json", "r", encoding='utf-8') as f1:
        data1 = json.load(f1)
    with open("data2.json", "r", encoding='utf-8') as f2:
        data2 = json.load(f2)
            
    data3 = []
    for item in data1:
        print(item)
        if item['userid'] not in [x['userid'] for x in data2]:
            data3.append(item)
    print(d3)
            
    with open('data3.json', 'w', encoding='utf-8') as nf:
        json.dump(data3, nf, indent=4)