View previous topic :: View next topic |
Author |
Message |
geek Halfop
Joined: 24 Oct 2008 Posts: 47
|
Posted: Mon Feb 27, 2023 7:37 am Post subject: extract json data |
|
|
hi,
I need help to handle the result of a [::http::data $token]
I think the data is in json format
I get a "structure" like this:
Code: | {"id":"cmpl-6oGBZmblablablablai9NTA","object":"text_completion","created":1677436637,"model":"text-davinci-003","choices":[{"text":"\n\nYes, today is Monday.","index":0,"logprobs":null,"finish_reason":null}],"usage":{"prompt_tokens":4,"completion_tokens":8,"total_tokens":12}} |
and I want to extract the text field "\n\nYes, today is Monday."
how can I do? |
|
Back to top |
|
 |
SpiKe^^ Owner

Joined: 12 May 2006 Posts: 826 Location: Tennessee, USA
|
Posted: Mon Feb 27, 2023 12:45 pm Post subject: json=>list=>json fix? |
|
|
That json data is somewhat odd, keeping the easiest plans from working:)
Let us assume you have that data you posted above saved in a variable named $jsondata
I might use something like this...
Code: |
package require json
set jdict [::json::json2dict $jsondata]
set todaytext [dict get [lindex [dict get $jdict choices] 0] text]
|
The value I think you seek should be saved in $todaytext _________________ SpiKe^^
Get BogusTrivia 2.06.4.7 at www.mytclscripts.com
or visit the New Tcl Acrhive at www.tclarchive.org
. |
|
Back to top |
|
 |
caesar Mint Rubber

Joined: 14 Oct 2001 Posts: 3767 Location: Mint Factory
|
Posted: Mon Feb 27, 2023 1:29 pm Post subject: |
|
|
Add a string map like:
Code: |
set todaytext [string map {\n {}} [dict get [lindex [dict get $jdict choices] 0] text]]
|
to remove the new lines.
Edit: Typo, it's string not strong map.  _________________ Once the game is over, the king and the pawn go back in the same box.
Last edited by caesar on Tue Feb 28, 2023 12:20 pm; edited 1 time in total |
|
Back to top |
|
 |
geek Halfop
Joined: 24 Oct 2008 Posts: 47
|
Posted: Mon Feb 27, 2023 1:59 pm Post subject: |
|
|
yes it works
thanks SpiKe^^ and caesar
I found a picture of how data is formatted:
 |
|
Back to top |
|
 |
caesar Mint Rubber

Joined: 14 Oct 2001 Posts: 3767 Location: Mint Factory
|
Posted: Tue Feb 28, 2023 12:19 pm Post subject: |
|
|
You can use an online app like JSON Viewer as well.  _________________ Once the game is over, the king and the pawn go back in the same box. |
|
Back to top |
|
 |
geek Halfop
Joined: 24 Oct 2008 Posts: 47
|
Posted: Tue Feb 28, 2023 1:34 pm Post subject: |
|
|
very useful
tnx |
|
Back to top |
|
 |
|