1

I have a JSON file with logs of music streams like in the format below (don't know how many exactly it's a few months of streams) and I want to find how many times a specific artist name or track name occurs in it. How would I do that?

 [
 {
  "endTime" : "2020-07-16 21:01",
  "artistName" : "Jackson Wang",
  "trackName" : "BAD BACK (feat. GoldLink)",
  "msPlayed" : 162194
 },
 {
  "endTime" : "2020-07-16 21:03",
  "artistName" : "Jackson Wang",
  "trackName" : "TITANIC (feat. Rich Brian)",
  "msPlayed" : 140934
 },
 {
  "endTime" : "2020-07-16 21:06",
  "artistName" : "Jackson Wang",
  "trackName" : "FADED",
  "msPlayed" : 157192
 },
 {
  "endTime" : "2020-07-16 21:09",
  "artistName" : "Kid Ink",
  "trackName" : "Same Day",
  "msPlayed" : 181447
 },
 {
  "endTime" : "2020-07-16 21:12",
  "artistName" : "Kid Ink",
  "trackName" : "Good Idea (feat. BIA)",
  "msPlayed" : 195936
 }
]
Nat
  • 11
  • 4

3 Answers3

2

If you are only interested in counting the occurrences of specific elements, the way that requires less code is as follows: a json can be loaded to be simply a string. You can convert the dictionary into a string and use the builtin string.count().

import json
with open('stack.json') as json_file:
    data = json.load(json_file)
print(str(data).count("Jackson Wang"))

Output

3
Wippo
  • 853
  • 8
  • 22
  • when i load the json file it sees it as a list instead of a dictionary. What do I do? – Nat Dec 20 '20 at 16:42
  • @Nat If you are talking about that json you have in the question, there is no error in having a list, indeed the file you loaded is a list of dictionaries. If after loading the file as in my answer you do `data[0]['msPlayed']` you will get 162194 – Wippo Dec 20 '20 at 18:08
  • @Nat The code in my previous comment proves that, as I said, the file is a list of dictionaries and that each dictionary, when properly accessed, preserves the characteristics of a python dict. If you have other problems with your json that are difficult to explain in a comment, consider opening another question. – Wippo Dec 20 '20 at 18:18
2

you can use counter to do that and enumrate to print that

import json
from collections import Counter

data = [
 {
  "endTime" : "2020-07-16 21:01",
  "artistName" : "Jackson Wang",
  "trackName" : "BAD BACK (feat. GoldLink)",
  "msPlayed" : 162194
 },
 {
  "endTime" : "2020-07-16 21:03",
  "artistName" : "Jackson Wang",
  "trackName" : "TITANIC (feat. Rich Brian)",
  "msPlayed" : 140934
 },
 {
  "endTime" : "2020-07-16 21:06",
  "artistName" : "Jackson Wang",
  "trackName" : "FADED",
  "msPlayed" : 157192
 },
 {
  "endTime" : "2020-07-16 21:09",
  "artistName" : "Kid Ink",
  "trackName" : "Same Day",
  "msPlayed" : 181447
 },
 {
  "endTime" : "2020-07-16 21:12",
  "artistName" : "Kid Ink",
  "trackName" : "Good Idea (feat. BIA)",
  "msPlayed" : 195936
 }
]

trackName_counter = Counter(item["trackName"] for item in data)

this will store the frequency of data in the traackName_counter

Pratik Agrawal
  • 405
  • 3
  • 17
2
  1. Create a json text file
  2. Read the file and create python objects from it
  3. analyse the python object and count things you want to count
  4. print results

Code:

with open("f.txt", "w") as f:
  f.write("""[
 {
  "endTime" : "2020-07-16 21:01",  "artistName" : "Jackson Wang",
  "trackName" : "BAD BACK (feat. GoldLink)",  "msPlayed" : 162194 },
 {
  "endTime" : "2020-07-16 21:03",  "artistName" : "Jackson Wang",
  "trackName" : "TITANIC (feat. Rich Brian)",  "msPlayed" : 140934 },
 {
  "endTime" : "2020-07-16 21:06",  "artistName" : "Jackson Wang",
  "trackName" : "FADED",  "msPlayed" : 157192 },
 {
  "endTime" : "2020-07-16 21:09",  "artistName" : "Kid Ink",
  "trackName" : "Same Day",  "msPlayed" : 181447 },
 {
  "endTime" : "2020-07-16 21:12",  "artistName" : "Kid Ink",
  "trackName" : "Good Idea (feat. BIA)",  "msPlayed" : 195936}]""" )

import json
from collections import Counter

with open("f.txt") as f:
    d = json.load(f)

c = Counter()
for thing in d:
  artist = thing["artistName"]
  track = thing["trackName"]
  c.update([artist])
  c.update([track])  

print(c)  

Output:

Counter({'Jackson Wang': 3, 
         'Kid Ink': 2, 
         'BAD BACK (feat. GoldLink)': 1, 
         'TITANIC (feat. Rich Brian)': 1, 
         'FADED': 1, 
         'Same Day': 1, 
         'Good Idea (feat. BIA)': 1})
Patrick Artner
  • 50,409
  • 9
  • 43
  • 69