Micheal
Micheal

Reputation: 2322

Printing JSON-formatted data in Tcl

Hi I need to print the following as it is in tcl.

{'root':[{'name':'Test', 'val':'1'},{'name':'Test2', 'val':'3'}]}

when i do the following I get an error

puts "{'root':[{'name':'$name', 'val':'$val'},{'name':'$name', 'val':'$val'}]}"

Is there a way i can print those in tcl?

ok I tried this now:

puts "{'root':\[{'name':'$name', 'val':'$id'}"

but that doesnt work either

Upvotes: 1

Views: 1613

Answers (2)

Carlos Tasada
Carlos Tasada

Reputation: 4444

Remember that the '[' means that you want to execute something, so you need to escape it.

puts "{'root':\[{'name':'$name', 'val':'$val'},{'name':'$name','val':'$val'}\]}"

With those changes, should work.

Also, you can find more examples of how to manage JSON in tcl here: http://wiki.tcl.tk/13419

Upvotes: 2

kostix
kostix

Reputation: 55483

It's just

puts {{'root':[{'name':'Test', 'val':'1'},{'name':'Test2', 'val':'3'}]}}

That is, put the whole string between { and } to make it "literal".

More info is in the tutorial.

Upvotes: 1

Related Questions