Hello, I have a custom function that converts a nested json list to a string. I send this string to the ChatGPT API for summarisation.
A problem that I am encountering is that when there are special characters present in the nested json list, I get an invalid json error from the ChatGPT API.
The complete custom function is provided below:
import 'dart:convert';
import 'dart:math' as math;
import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:intl/intl.dart';
import 'package:timeago/timeago.dart' as timeago;
import '/flutter_flow/lat_lng.dart';
import '/flutter_flow/place.dart';
import '/flutter_flow/uploaded_file.dart';
import '/flutter_flow/custom_functions.dart';
import '/auth/firebase_auth/auth_util.dart';
String? jsonListToString(
List<dynamic>? jsonList,
String? separator,
) {
/// MODIFY CODE ONLY BELOW THIS LINE
if (jsonList == null || separator == null) {
return null;
}
final List<String> stringList = [];
for (final dynamic item in jsonList) {
if (item is List) {
final String? nestedListString = jsonListToString(item, separator);
if (nestedListString != null) {
stringList.add(
nestedListString.replaceAll('"', '\\"').replaceAll("\\", "\\\\"));
}
} else if (item is Map) {
final String? nestedMapString =
jsonEncode(item).replaceAll('"', '\\"').replaceAll("\\", "\\\\");
if (nestedMapString != null) {
stringList.add(nestedMapString);
}
} else {
final String? itemString =
item?.toString().replaceAll('"', '\\"').replaceAll("\\", "\\\\");
if (itemString != null) {
stringList.add(itemString);
}
}
}
return stringList.join(separator);
/// MODIFY CODE ONLY ABOVE THIS LINE
}
Been stuck on this for a while, would appreciate any help with this. Thanks!