Extract the 'content' value

I need to pass the following and return the contents.

  "id": "xxx",
  "object": "chat.completion",
  "created": 1705715021,
  "model": "gpt-4-1106-preview",
  "choices": [
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "The term \"CQC\" can refer to different organizations or concepts, depending on the context. Here are a few possible meanings:\n\n1. **Care Quality Commission (CQC)**: This is an executive non-departmental public body of the Department of Health and Social Care in the United Kingdom. It was established in 2009 to regulate and inspect health and social care services in England. The CQC's purpose is to ensure that hospitals, care homes, dental and general practices, and other care services in England provide people with safe, effective, compassionate, and high-quality care.\n\n2. **Close Quarters Combat (CQC)**: This term is used to describe hand-to-hand combat or the use of small arms and blunt or bladed weapons which might be used in a tight space or in a military context. It is a concept that is both taught to military personnel and popularized in video games and movies.\n\n3. **Central Queensland University (CQUniversity)**: In the context of education, particularly in Australia, CQC might be mistaken for CQUniversity, which is a public university based in Central Queensland.\n\nTo provide you with the most accurate and relevant information, please specify the context or industry you're inquiring about."
      "logprobs": null,
      "finish_reason": "stop"
  "usage": {
    "prompt_tokens": 23,
    "completion_tokens": 251,
    "total_tokens": 274
  "system_fingerprint": "fp_04de91a479"

What's the problem you're having? Can you provide a code snippet we can help with? Looks like regular JSON, no?

Here's a snippet from the Template Project where I parse this same kind of JSON. But there's about a hundred ways to go about doing this, depending on what else you want to do with the JSON or the output. Here, the JSON text is loaded into a TJSONObject called ResponseJSON and the "content" value is extracted.

      // Long way to check that we've got JSON in an expected structure
      if (ResponseJSON <> nil) and
         ((ResponseJSON.GetValue('choices')) <> nil) and
         ((ResponseJSON.GetValue('choices') as TJSONArray)[0] <> nil) and
         (((ResponseJSON.GetValue('choices') as TJSONArray)[0] as TJSONObject).GetValue('message') <> nil) and
         ((((ResponseJSON.GetValue('choices') as TJSONArray)[0] as TJSONObject).GetValue('message') as TJSONObject).GetValue('content')  <> nil) then
        // Add response to the ChatWindow
        AddChatResponse(((((ResponseJSON.GetValue('choices') as TJSONArray)[0] as TJSONObject).GetValue('message') as TJSONObject).GetValue('content') as TJSONString).value, QueryTime, ResponseStatus, ResponseIcon);

You could likely do this with one JavaScript line, and you may want to be more careful about handling JSON where the structure isn't guaranteed - maybe there is no content returned, or maybe multiple content entries appear in the array.

Thanks I got thank working.

1 Like