Home > Blockchain >  Json count unique values
Json count unique values

Time:09-25

I have this json file:

[
  {
    "@timestamp": "",
    "userID": "",
    "destinationUserName": "",
    "message": ": 12,050",
    "name": "Purge Client Events"
  },
  {
    "@timestamp": "",
    "userID": "",
    "destinationUserName": "",
    "message": "",
    "name": ""
  },
  {
    "@timestamp": "",
    "userID": "",
    "destinationUserName": "",
    "message": "",
    "name": ""
  },
  {
    "@timestamp": "",
    "userID": "",
    "name": "",
    "sourceUserName": "",
    "deviceAction": ""
  }
]

I am looking for a solution in which I can loop over all the file, and count the unique values for UserID and return that value printed.

I found different solution but non of them worked for me and I am completely stuck.

So far this is my code, its just a formatter that convert the file into a json format.

After that I tried to check the length of the file and loop over it appending unique elements.

import json
to_queue = []
def structure_json():
    with open("file.json", "r ") as f:
        old = f.read()
        f.seek(0)  # rewind
        # save to the old string after replace
        new = old.replace('}{', '},{')
        f.write(new)
        tmps = '['   str(new)   ']'
        json_string = json.loads(tmps)
        for key in json_string:
            to_queue.append(key)
        f.close
    with open('update_2.json', 'w') as file:
        json.dump(json_string, file, indent=2)
        size=len(file["UserID"])
        uniqueNames = [];
        for i in range(0,size,1):
            if(file["UserID"] not in uniqueNames):
                uniqueNames.append(file["UserID"]);
                print(uniqueNames)

structure_json()
print(to_queue)

But I get the following error:

Traceback (most recent call last):
  File "format.py", line 24, in <module>
    structure_json()
  File "format.py", line 17, in structure_json
    size=len(file["UserID"])
TypeError: '_io.TextIOWrapper' object is not subscriptable


Please any help will be much appreciated. Thank you so much for any help, and if you need any more info just let me know

CodePudding user response:

Open the file and load the content. Then you can iterate over list of dicts and crate set of all values for key userID. Note, if any missing key it will yield None and will affect the count ( 1).

import json

with open('your_file.json') as f:
    data = json.load(f)
    users = set(item.get('userID') for item in data)
    print(len(users))
    print(users)
  • Related