Python code to convert the aw-category-export.json to a string queries will accept

I couldn’t find anything that does this anywhere, nor the toml file referenced in some examples.

This code takes the most recent aw-category-export.json from a folder specified (i used downloads) and converts it to a string to insert into queries.

import re
import os

def convert_categorisation(path_with_space):
#inorder to make the raw string work ensure there is a space before the final "  EG:
# r"C:\Users\NAME\Downloads\ "

downloads_path = path_with_space[:-1]

file_location = downloads_path + 'aw-category-export.json'

matches = 0
#searches download path for the most recent category export.
for root, dirs, files in os.walk(downloads_path):
    for file in files:
        if re.match('aw-category-export \(\d+\).json', file):
            matches += 1
if matches != 0:
    file_location = downloads_path + 'aw-category-export (%s).json'%matches

final_categories = []

#picks apart the category json and returns it in a format the queries want to work with.
with open(file_location, 'r') as file:
    data = json.load(file)

    for entry in data["categories"]:
        category = ()
        if entry['rule']['type'] != None:
            category = category + (entry['name'],)
            category = category + (entry['rule'],)


I hope this is helpfull to someone, if you have feedback i’d love to hear it.

I don’t quite get what you are trying to do but it may help loading the json as json and possibly adding in pandas.

Here is a jupyter notebook I created to make sure the code I write below works.

import json
import pandas as pd

# This variable hold the entire json file
my_bucket = json.load(open('/home/paul/Downloads/aw-bucket-export_aw-stopwatch.json'))

# This is where the events are stored in the json file
events = my_bucket["buckets"]['aw-stopwatch']["events"]

# This is how I loaded the events into pandas
events_json_string = json.dumps(events)
df = pd.read_json(events_json_string)