Hi, please consider the code below.
import json
var field1, field2, field3 : str
var jz : JsonNode
field1 = jz["node1"].getStr
field2 = jz["node2"].getStr
field3 = jz["node3"].getStr
Sometimes the nodes node1, node2 and/or node3 are available at my jz content. But just sometimes... If one of them are absent, an unhandled exception will occurs.
In Nim, what is the equivalent command of Python's dict.get(key[, default]) ? I would like to checks if the specified key exists in the JsonNode. If it does, then returns the value of that key, if not kindly give me let's say a blank value.
Best
I'm learning Nim myself by trying to write this, seems working
import json
var field1, field2, field3 : string
var jz : JsonNode = %* {"node1": "Node1Value","node3":nil}
template nodeAsStr(n:JsonNode,key:string,default:string="*NoValue*"): string =
if n!=nil and n.hasKey(key):
n[key].getStr(default)
else:
"*NoKey*"
field1 = jz.nodeAsStr("node1")
field2 = jz.nodeAsStr("node2")
field3 = jz.nodeAsStr("node3")
echo "field1:", field1
echo "field2:", field2
echo "field3:", field3
field1 = jz{"node1"}.getStr
@sky_khan, I agree with you. The more examples the better!
I am also learning Nim ... one of the main problems I had (and I have) is precisely the absence of examples in the documentation.
In addition, I would kindly ask a favor for the older staff - please create more examples in the documentation. I'm sure this will help people a lot !!
Ironically, it is newbies who recognize what is missing.
Add a PR as you learn, so others can benefit. It also gets you familiar with contributing to Nim.
There have been many suggestions of a cookbook for easy cut and paste of example code. It hasn't really gained traction, unfortunately.
Another help may be Rosetta Code (although it doesn't have a lot of json examples)
Issue raised here: https://github.com/nim-lang/Nim/issues/6531
Remember, with 4 PRs this month, you get a Hacktoberfest shirt ;)
To retrieve the value of "key" you can do the following:
doAssert jsonNode["key"].getFNum() == 3.14
The [] operator will raise an exception when the specified field does not exist. If you wish to avoid this behaviour you can use the {} operator instead, it will simply return nil when the field is not found. The get-family of procedures will return a default value when called on nil.
It's a well known fact people do not read text on the screen (I'm serious) but there are limits to what we can do about it.
Thank you guys !
@mratsim, off topic: https://twitter.com/DeepMindAI/status/920696139657240576 Cool !!
@Araq, English is not my native language. When I started programming as a child, my all English knowledge was consisting of "What is this? This is a pencil" :) So I learned programming by reading and tinkering other people's code and learned English by trying to understand programming manuals, all by myself. My English is still far from perfect and I'm still not fond of reading wall of text. I dont think I'm alone. When I looked json document, beginning of that text was looking like a preamble and not useful so I jumped directly to procs and got lost among unorganized stacks of them.
Anyway,I'll look into document generation process of Nim and I'll see if I can do any better.