JSONDecodeError: Extra data: line 2 column 1 (char 863)
JSONDecodeError: Expecting value: line 2 column 1 (char 2)
json에 저장이 잘못되어
[
{
input1 : answer1
input2 : answer2
}
]
로 저장되었는데
각 줄마다 읽어와서 데이터 없다고 에러발생 (그러니까위 코드 기준 [ 만 나온셈)
구분자 separators를 설정하고 [] 대신 \n으로 구분해서 json 저장하여
{"is_spoiler":true,"review_text":"Awesome movie\nnyc\nvery goodddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd"}
이 형태로 잘 저장했다!
아래는 실제 활용 코드
import json
# Define file paths
input_file_path = "C:/Users/SSAFY/Desktop/Common_AI/movieData/IMDB_reviews.json" # Input file containing JSON lines
output_file_path = 'C:/Users/SSAFY/Desktop/Common_AI/movieData/filtered_reviews.json' # Output file to save the filtered JSON lines
# Open the input file and read it line by line
with open(input_file_path, 'r') as input_file:
with open(output_file_path, 'w') as output_file:
for line in input_file:
try:
# Parse the JSON object from the line
json_obj = json.loads(line)
# Extract specific fields with default values
transformed_obj = {
"is_spoiler": json_obj.get("is_spoiler", ""),
"review_text": json_obj.get("review_text", "").strip()
}
if 'review_text' in transformed_obj and len(transformed_obj['review_text']) <= 700:
# Write each transformed JSON object as a single line in the output file
json.dump(transformed_obj, output_file, separators=(',', ':'))
output_file.write('\n') # Add a newline character after each JSON object
except json.JSONDecodeError as e:
print(f"Error decoding JSON line: {e}")
print(f'Transformed data saved to {output_file_path}')
import json
def txt_to_json(txt_filename, json_filename):
# Read the entire text file
with open(txt_filename, 'r', encoding='utf-8') as txt_file:
with open(json_filename, 'w', encoding='utf-8') as json_file:
content = txt_file.read().strip()
# Split content by double newlines to get individual reviews
lines = content.split('\n\n')
for line in lines:
# Create a list of dictionaries for each review
data = {'is_spoiler': True, 'review_text': line.strip()}
# Write the list of dictionaries to a JSON file
json.dump(data, json_file, ensure_ascii=False, separators=(',', ':'))
json_file.write('\n')
# Usage
txt_to_json(r'C:\Users\SSAFY\Desktop\104_reviews.txt', r'C:\Users\SSAFY\Desktop\data.json')