GPT sabit çıktı vermeyecektir mutlaka bir yerde bozacaktır. Aşağıdaki gibi basit bir kodla içindeki jsonu alabilirsiniz.
function extractJSON(str) {
const start = str.indexOf('{');
const end = str.lastIndexOf('}');
if (start !== -1 && end !== -1) {
const jsonStr = str.slice(start, end + 1);
try {
const jsonObj = JSON.parse(jsonStr);
return jsonObj;
} catch (e) {
console.error('Invalid JSON in string');
}
} else {
console.error('No JSON found in string');
}
}
const str = 'Some text before {"key": "value"} some text after';
const jsonObj = extractJSON(str);
console.log(jsonObj); // Outputs: { key: 'value' }