-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdifjson_to_csv.py
More file actions
216 lines (184 loc) · 7.54 KB
/
Copy pathdifjson_to_csv.py
File metadata and controls
216 lines (184 loc) · 7.54 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
"""
Convert JSON files to CSV format
Generates CSV with DOI, dataset name, and file metadata columns
"""
import os
import sys
import argparse
from typing import List, Dict, Any
from utils import (
load_json_file,
save_csv_file,
get_json_files_from_directory
)
def extract_doi(json_data: Dict[str, Any]) -> str:
"""Extract a DOI from common Dataverse JSON field names."""
if "datasetPersistentId" in json_data and json_data["datasetPersistentId"]:
return json_data["datasetPersistentId"]
if "datasetVersion" in json_data and isinstance(json_data["datasetVersion"], dict):
doi = json_data["datasetVersion"].get("datasetPersistentId", "")
if doi:
return doi
if "persistentUrl" in json_data and json_data["persistentUrl"]:
return json_data["persistentUrl"].replace("https://doi.org/", "doi:")
if "identifier" in json_data and json_data["identifier"]:
return json_data["identifier"]
return ""
def extract_title_from_citation(json_data: Dict[str, Any]) -> str:
"""Extract dataset title from nested citation metadata."""
if "datasetName" in json_data and json_data["datasetName"]:
return json_data["datasetName"]
if "title" in json_data and json_data["title"]:
return json_data["title"]
dataset_version = json_data.get("datasetVersion") or json_data.get("latestVersion")
if isinstance(dataset_version, dict):
citation = dataset_version.get("metadataBlocks", {}).get("citation", {})
fields = citation.get("fields") or []
for field in fields:
if field.get("typeName") == "title":
value = field.get("value")
if isinstance(value, str):
return value
if isinstance(value, dict):
return value.get("value", "")
if isinstance(value, list) and value:
first = value[0]
if isinstance(first, dict):
return first.get("value", "")
return str(first)
return "Unknown Dataset"
def extract_files(json_data: Dict[str, Any]) -> List[Dict[str, Any]]:
"""Return file entries from either modern Dataverse or legacy JSON formats."""
# Check datasetVersion.files first (nested structure from Dataverse API)
dataset_version = json_data.get("datasetVersion") or json_data.get("latestVersion")
if isinstance(dataset_version, dict):
return dataset_version.get("files", [])
# Check top-level data/files
if "data" in json_data and isinstance(json_data["data"], list):
return json_data["data"]
if "files" in json_data and isinstance(json_data["files"], list):
return json_data["files"]
return []
def get_file_info(file_info: Dict[str, Any]) -> Dict[str, Any]:
"""Normalize file metadata from both `data` and `files` JSON structures."""
data_file = file_info.get("dataFile") if isinstance(file_info.get("dataFile"), dict) else file_info
return {
"id": data_file.get("id", ""),
"label": file_info.get("label", "") or data_file.get("filename", ""),
"description": file_info.get("description", ""),
"directoryLabel": file_info.get("directoryLabel", ""),
"filesize": data_file.get("filesize", ""),
"dataType": data_file.get("dataType", data_file.get("contentType", ""))
}
def json_to_csv_converter(json_files: List[str], output_csv: str, dataset_names: Dict[str, str] = None) -> None:
"""
Convert multiple JSON files to a single CSV file
Args:
json_files: List of paths to JSON files
output_csv: Output CSV file path
dataset_names: Optional dictionary mapping DOI to dataset name
"""
csv_rows = []
dataset_names = dataset_names or {}
for json_file in json_files:
try:
print(f"Processing: {json_file}")
json_data = load_json_file(json_file)
#EXTRA 2 LINES
if "data" in json_data and isinstance(json_data["data"],dict):
json_data = json_data["data"]
# Extract DOI
doi = extract_doi(json_data)
if not doi:
print(f" Warning: No DOI found in {json_file}")
continue
# Get dataset name
dataset_name = dataset_names.get(doi) or extract_title_from_citation(json_data)
# Extract file information
files = extract_files(json_data)
for file_info in files:
normalized = get_file_info(file_info)
row = {
"DOI": doi,
"dataset_name": dataset_name,
"file_id": normalized.get("id", ""),
"file_label": normalized.get("label", ""),
"file_description": normalized.get("description", ""),
"file_path": normalized.get("directoryLabel", ""),
"file_size": str(normalized.get("filesize", "")),
"original_description": normalized.get("description", ""),
"new_description": "",
"new_file_path": "",
"status": "pending"
}
csv_rows.append(row)
# If no files, still create a row for dataset metadata
if not files:
row = {
"DOI": doi,
"dataset_name": dataset_name,
"file_id": "",
"file_label": "",
"file_description": "",
"file_path": "",
"file_size": "",
"original_description": "",
"new_description": "",
"new_file_path": "",
"status": "no_files"
}
csv_rows.append(row)
except Exception as e:
print(f" Error processing {json_file}: {str(e)}")
continue
if csv_rows:
fieldnames = [
"DOI",
"dataset_name",
"file_id",
"file_label",
"file_description",
"file_path",
"file_size",
"original_description",
"new_description",
"new_file_path",
"status"
]
save_csv_file(csv_rows, output_csv, fieldnames)
print(f"\nSuccessfully converted {len(csv_rows)} rows to {output_csv}")
else:
print("No data to convert")
def main():
parser = argparse.ArgumentParser(
description="Convert JSON metadata files to CSV format"
)
parser.add_argument(
"--input-dir",
type=str,
default="./data/json_templates",
help="Directory containing JSON files (default: ./data/json_templates)"
)
parser.add_argument(
"--output-csv",
type=str,
default="./data/metadata.csv",
help="Output CSV file path (default: ./data/metadata.csv)"
)
parser.add_argument(
"--json-file",
type=str,
help="Single JSON file to convert (alternative to --input-dir)"
)
args = parser.parse_args()
# Determine which files to process
if args.json_file:
json_files = [args.json_file]
else:
json_files = get_json_files_from_directory(args.input_dir)
if not json_files:
print(f"No JSON files found in {args.input_dir}")
sys.exit(1)
json_to_csv_converter(json_files, args.output_csv)
if __name__ == "__main__":
main()