Nat
Nat

Reputation: 11

How to find how many times a key value contains the same value in a json file using python

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
 }
]

Upvotes: 1

Views: 64

Answers (3)

Wippo
Wippo

Reputation: 963

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

Upvotes: 2

Patrick Artner
Patrick Artner

Reputation: 51673

  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})

Upvotes: 2

Pratik Agrawal
Pratik Agrawal

Reputation: 368

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

Upvotes: 2

Related Questions