Hello all,
I want to use json as a layer for (gui-)definitions and data but i am missing runnable examples on some subjects.
I understand how i can read and write json-files, and that it is a tree of nodes of different types.
I can add a key-value pair with:
myjnob.add("name", %"Peter")
echo myjnob
I can also overwrite with Add, but what if there are non-unique keys? I guess they are not recommended in Jobjects? Is there another way of editing values?
Also I can delete kv-pairs with the Delete-command:
myjnob.delete("city")
echo myjnob
I suppose repetitive data/keys must be put in the Jarrays (which are not /can not be parsed for keys apparently). But the question remaining is how to edit Jarrays. I can add items with Add but i dont know how to edit or delete items. Does somebody?
Thanx in advance.
Thanks for the quick response however I still get errors:
arrayjnob.delete(2)
produces compile-error:
test_11228_json_opbouw.nim(52, 10) Error: type mismatch: got <JsonNode, int literal(2)> but expected one of:
1 other mismatching symbols have been suppressed; compile with --showAllMismatches:on to see them
expression: delete(arrayjnob, 2)
And the second error: arrayjnob.del(2)
produces compile-error:
test_11228_json_opbouw.nim(52, 10) Error: type mismatch: got <JsonNode, int literal(2)> but expected one of:
expression: del(arrayjnob, 2)
Thanx planetis, that did the trick!
delete(arrayjnob.elems, 2)
or:
arrayjnob.elems.delete(2)
Apparently in that case it can be handled as a normal sequence (of jsonnodes).
For other straying people to learn: To insert i can do:
arrayjnob.elems.insert(%10,0)
The %-sign because you must insert a jsonnode.
Concerning uniqueness: Now iIt seems logical to put repetitive stuff in the jarray and keep all the other keys unique.
If you feel ambitious, you might try adding to the standard json library with a pull request. Not having a
func delete*(s: JsonNode; index: Natural)
function seems a bit out-of-place given the corresponding add.
BTW, as a bonus, I think you could also access a read-only copy of the array with getElems(). This will default to an empty sequence should it not actually be an array (rather than throw a run-time error.)
Hi John, looked at:
https://github.com/nim-lang/Nim/issues/15001
It is apparently in a techno-political process which I will not enter. Two guys were trying to merge their stuff in the devel-branche unsuccesfully, as it seems. Bigger design-questions are being put forward by bigger bosses, which are a little to much for me at this time.
By the way I think it was you who did the jester-videos (?) that inspired me to write a program: https://github.com/some-avail/readibl
I use webgui_def.nim and jo_htmlgen.nim to generate certain html-controls, but I want to go over to json to store the gui-defs, so that it is more flexible, and maybe in the future database-data can be added to the json html-defs.
I am however interested in doing something cooperatively (as opposed to individually), but maybe I must find a smaller project first.
Greets, Joris.