egghelp.org community Forum Index
[ egghelp.org home | forum home ]
egghelp.org community
Discussion of eggdrop bots, shell accounts and tcl scripts.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

extract json data

 
Post new topic   Reply to topic    egghelp.org community Forum Index -> Scripting Help
View previous topic :: View next topic  
Author Message
geek
Halfop


Joined: 24 Oct 2008
Posts: 47

PostPosted: Mon Feb 27, 2023 7:37 am    Post subject: extract json data Reply with quote

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
View user's profile Send private message
SpiKe^^
Owner


Joined: 12 May 2006
Posts: 826
Location: Tennessee, USA

PostPosted: Mon Feb 27, 2023 12:45 pm    Post subject: json=>list=>json fix? Reply with quote

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
View user's profile Send private message Visit poster's website
caesar
Mint Rubber


Joined: 14 Oct 2001
Posts: 3767
Location: Mint Factory

PostPosted: Mon Feb 27, 2023 1:29 pm    Post subject: Reply with quote

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. Laughing
_________________
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
View user's profile Send private message
geek
Halfop


Joined: 24 Oct 2008
Posts: 47

PostPosted: Mon Feb 27, 2023 1:59 pm    Post subject: Reply with quote

yes it works

thanks SpiKe^^ and caesar


I found a picture of how data is formatted:

Back to top
View user's profile Send private message
caesar
Mint Rubber


Joined: 14 Oct 2001
Posts: 3767
Location: Mint Factory

PostPosted: Tue Feb 28, 2023 12:19 pm    Post subject: Reply with quote

You can use an online app like JSON Viewer as well. Smile
_________________
Once the game is over, the king and the pawn go back in the same box.
Back to top
View user's profile Send private message
geek
Halfop


Joined: 24 Oct 2008
Posts: 47

PostPosted: Tue Feb 28, 2023 1:34 pm    Post subject: Reply with quote

very useful

tnx
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    egghelp.org community Forum Index -> Scripting Help All times are GMT - 4 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Forum hosting provided by Reverse.net

Powered by phpBB © 2001, 2005 phpBB Group
subGreen style by ktauber