-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfix_json.py
More file actions
39 lines (32 loc) · 1.32 KB
/
Copy pathfix_json.py
File metadata and controls
39 lines (32 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import sqlite3
import json
def fix_table(conn, table):
cols = ['metadata_blob', 'draft_metadata_blob', 'authors', 'associated_terms']
# publications might not have draft_metadata_blob, check schema
cursor = conn.cursor()
cursor.execute(f"PRAGMA table_info({table})")
schema_cols = [c[1] for c in cursor.fetchall()]
actual_cols = [c for c in cols if c in schema_cols]
if not actual_cols:
return
rows = conn.execute(f"SELECT id, {', '.join(actual_cols)} FROM {table}").fetchall()
for row in rows:
row_id = row[0]
updates = {}
for i, col in enumerate(actual_cols):
val = row[i+1]
if val is not None and isinstance(val, str) and val.startswith('"'):
try:
decoded = json.loads(val)
updates[col] = decoded
except:
pass
if updates:
set_clause = ", ".join([f"{k} = ?" for k in updates.keys()])
values = list(updates.values()) + [row_id]
conn.execute(f"UPDATE {table} SET {set_clause} WHERE id = ?", values)
conn = sqlite3.connect('cruk_datahub.db')
for table in ['datasets', 'projects', 'publications']:
fix_table(conn, table)
conn.commit()
print("Fixed double-encoded JSON in local database.")