Android Question Lost in ChatGPT JSON

JackKirk

Well-Known Member
Licensed User
Longtime User
JSON is a weak suit of mine.

I have a JSON string:
{"id":"chatcmpl-6t2JQdgU1ypn0ayhONAkE6bAEoGkz","object":"chat.completion","created":1678574948,"model":"gpt-3.5-turbo-0301","usage":{"prompt_tokens":25,"completion_tokens":110,"total_tokens":135},"choices":[{"message":{"role":"assistant","content":"Ahoy matey, ye be askin' a great question. The worst investment be ones that promise quick riches without flappin' yer sails too much, like the \"get rich quick\" schemes, ponzi schemes Or pyramid schemes. These scams be all about misuse of trust And deceivin' the inexperienced. They be luring investors with high promised returns, but in the end, they just take yer doubloons and disappear into the horizon. Stay away from such crooks and keep yer treasure safe, me hearty!"},"finish_reason":"stop","index":0}]}

How do I parse this to extract the message content starting with "Ahoy matey..." and ending with "...me hearty!" ?

If someone can help with this I will be able to show how to interact with ChatGPT - the real deal.

Thanks...
 

JohnC

Expert
Licensed User
Longtime User
I simply asked ChatGPT your question, and below is its *exact* response...

To extract the message content starting with "Ahoy matey..." and ending with "...me hearty!" from the given JSON string in B4X, you can follow these steps:
  1. Add the OkHttpUtils2 library to your project using the B4X IDE's Libraries Manager.
  2. Parse the JSON string into a Map using the Json library's Parse method.
  3. Access the "choices" key in the Map to get a List of choices.
  4. Access the first choice (index 0) in the List.
  5. Access the "message" key in the choice to get a Map with the message content and role.
  6. Access the "content" key in the message Map to get the actual message content.
  7. Extract the message content starting with "Ahoy matey..." and ending with "...me hearty!" using the SubString2 method.
Here's some sample B4X code that does this:
B4X:
Sub Process_Globals
    Private http As OkHttpUtils2
    Private Json As JSONParser
End Sub

Sub AppStart
    http.Initialize("http")
    Json.Initialize
    Dim json_string As String = "{\"id\":\"chatcmpl-6t2JQdgU1ypn0ayhONAkE6bAEoGkz\",\"object\":\"chat.completion\",\"created\":1678574948,\"model\":\"gpt-3.5-turbo-0301\",\"usage\":{\"prompt_tokens\":25,\"completion_tokens\":110,\"total_tokens\":135},\"choices\":[{\"message\":{\"role\":\"assistant\",\"content\":\"Ahoy matey, ye be askin' a great question. The worst investment be ones that promise quick riches without flappin' yer sails too much, like the \\\"get rich quick\\\" schemes, ponzi schemes Or pyramid schemes. These scams be all about misuse of trust And deceivin' the inexperienced. They be luring investors with high promised returns, but in the end, they just take yer doubloons and disappear into the horizon. Stay away from such crooks and keep yer treasure safe, me hearty!\"},\"finish_reason\":\"stop\",\"index\":0}]}"

    ' Parse the JSON string into a Map
    Dim data As Map = Json.Parse(json_string)
 
    ' Access the message content
    Dim message_content As String = data.Get("choices").Get(0).Get("message").Get("content")
 
    ' Extract the message content starting with "Ahoy matey..." and ending with "...me hearty!"
    Dim start As Int = message_content.IndexOf("Ahoy matey")
    Dim end As Int = message_content.IndexOf("me hearty!") + Len("me hearty!")
    Dim extracted_message As String = message_content.SubString2(start, end)

    Log(extracted_message) ' Output: Ahoy matey, ye be askin' a great question. The worst investment be ones that promise quick riches without flappin' yer sails too much, like the "get rich quick" schemes, ponzi schemes Or pyramid schemes. These scams be all about misuse of trust And deceivin' the inexperienced. They be luring investors with high promised returns, but in the end, they just take yer doubloons and disappear into the horizon. Stay away from such crooks and keep yer treasure safe, me hearty!
End Sub
 
Last edited:
Upvote 0

JohnC

Expert
Licensed User
Longtime User
ChatGPT has already helped save me HOURS of time working on code!
 
Upvote 0
Top