[{"created":"20251114135218018","text":"<<tabs \"[[JSON Ops]] [tag<currentTiddler>sortan[]]\" \"JSON Ops\" \"$:/state/json-question\" >> ","title":"JSON Question","modified":"20251114171607227","type":"","query":"[[JSON Question]] [[JSON Ops]] [[JSON Example procedure]] MyJson   [tag[JSON Question]sortan[]]"},{"created":"20251114135509770","text":"This is a series of questions and answers from [[a thread on talk.tiddlywiki.org|https://talk.tiddlywiki.org/t/14458]] asking about how to manipulate JSON tiddlers.  We try to answer them in a consistent fashion., but we should make a few notes first.\n\n!! All Answers start from the same input tiddler:\n\n!!! [[MyJson]]\n\n{{MyJson}}\n\nSo a filter that looks like this:\n\n```\n[{MyJson}] \n  +[jsonset:object[A third tiddler]] \n  +[jsonset[A third tiddler],[icon],[icon3]]\n  +[jsonset[A third tiddler],[color],[#facade]]\n```\n\nstarts by getting the `text` field of the tiddler `MyJson` as a string, and that's what we'll be maniuplating.\n\n\n\n!!  We create a new JSON string\n\nWhen we do this manipulation, we ''don't directly alter the content of that tiddler''.  We create a new JSON string.  This is important to note.  It takes a second step to //update// the original, if that's what we want. \n\nFor a second step, we could do this:\n\n<pre><code>&lt;$button>Add new object to ~MyJson\n  &lt;$let result=<$text text=\"{{{\"/> \n    [{~MyJson}] \n      +[jsonset:object[A third tiddler]] \n      +[jsonset[A third tiddler],[icon],[icon3]]\n      +[jsonset[A third tiddler],[color],[#facade]]\n      +[format:json[4]]\n    }}} >\n    @@&lt;$action-setfield $tiddler=\"~MyJson\" $value=&lt;&lt;result>> />@@\n  &lt;/$let>\n&lt;$button></code></pre>\n\nWe could combine these into a single action like this:\n\n<pre><code>&lt;$button>\n  Add new object to ~MyJson\n  @@&lt;$action-setfield $tiddler=\"MyJson\" $value=@@<$text text=\"{{{\"/> \n    [{~MyJson}] \n      +[jsonset:object[A third tiddler]] \n      +[jsonset[A third tiddler],[icon],[icon3]]\n      +[jsonset[A third tiddler],[color],[#facade]]\n      +[format:json[4]]\n  }}} />\n&lt;$button></code></pre>\n\n\n!! The parameters can be any wikitext values.\n\nWe are not limited to hard-coded values like `[A third tiddler]`, `[icon]` and `[#facade]`.  We can use other reference, such as variables `<title>` or `<medium-pink>`  or tiddler field references such as `{!!main-icon}` or `{some tiddler!!icon}`.\n\nSo we might include a line like this:\n\n<pre><code>      +[jsonset@@{!!title}@@,[color],@@&lt;medium-pink>@@]</code></pre>\n\n\n!! Little white-space.\n\nBy default, there is no unnecessary white space in JSON output.  I add `+[format:json[4]]` to the display of of each filter here so they are easy to read.  But in general, this is only necessary if the result is going to be put somewhere you're going to be viewing the JSON directly.  Otherwise, default output like this is perfectly usable for tools that work with JSON:\n\n```\n{\"A tiddler\":{\"icon\":\"my-icon\",\"color\":\"#fedcba\"},\"Another tiddler\":{\"icon\":\"another-icon\",\"color\":\"#abcdef\"},\"A third tiddler\":{\"icon\":\"icon3\",\"color\":\"#facade\"}}\n```\n\n!! The object in a JSON string ''do not'' have to be tiddlers\n\nThe questions seemed to assume that what you store in JSON is always tiddlers.  While there are many such uses across TW, what is stored in JSON strings can be arbitrary data.\n\nThis would be perfectly legitimate JSON data: \n\n```json\n{\n    \"books\":[\n        {\n            \"isbn\":\"9781593279509\",\n            \"title\":\"Eloquent JavaScript, Third Edition\",\n            \"subtitle\":\"A Modern Introduction to Programming\",\n            \"author\":\"Marijn Haverbeke\",\n            \"published\":\"2018-12-04\",\n            \"publisher\":\"No Starch Press\",\n            \"description\":\"JavaScript lies at the heart of almost every modern web application...\",\n        },\n        {\n            \"isbn\":\"9781491943533\",\n            \"title\":\"Practical Modern JavaScript\",\n            \"subtitle\":\"Dive into ES6 and the Future of JavaScript\",\n            \"author\":\"Nicolás Bevacqua\",\n            \"published\":\"2017-07-16\",\n            \"publisher\":\"O'Reilly Media\",\n            \"description\":\"To get the most out of modern JavaScript, you need...\",\n        }\n    ]\n}\n```\n\nNote that this isn't tiddler data, and there is more than one level of nesting.\n\n\n","tags":"","title":"JSON Ops","modified":"20251114203732507"},{"created":"20251114151640682","text":"\\procedure ex(fltr)\n\n!!! Code\n  \n<pre><code><<fltr>></code></pre>\n\n!!! Result\n  \n<$let result={{{ [subfilter<fltr>] }}}>\n  <pre><$text text={{{ [<result>format:json[4]] }}} /></pre>\n</$let>\n\n\\end ex\n","tags":"$:/tags/Global","title":"JSON Example procedure","modified":"20251114154324595","code-body":"yes"},{"created":"20251114132518575","text":"{\n    \"A tiddler\": {\n        \"icon\": \"my-icon\",\n        \"color\": \"#fedcba\"\n    },\n    \"Another tiddler\": {\n        \"icon\": \"another-icon\",\n        \"color\": \"#abcdef\"\n    }\n}","tags":"","title":"MyJson","modified":"20251114143036350","type":"application/json"},{"created":"20251114132615973","text":"!! How can I add a tiddler with a set of defined fields inside the json data tiddler?\n\nWe do this by first adding an empty object, and then setting its properties individually:\n\n<<ex \"\"\"[{MyJson}] \n  +[jsonset:object[A third tiddler]] \n  +[jsonset[A third tiddler],[icon],[icon3]]\n  +[jsonset[A third tiddler],[color],[#facade]]\n\"\"\" >>\n\n","tags":"[[JSON Question]]","title":"Question 1","modified":"20251114193923433","caption":"Add Node"},{"created":"20251114150507213","text":"!! How can I add a field name and value to a tiddler already in the json data tiddler?\n\n<<ex \"\"\"[{MyJson}] \n  +[jsonset[A tiddler],[favorite-animal],[manatee]]\n\"\"\" >>\n\n","tags":"[[JSON Question]]","title":"Question 2","modified":"20251114152621001","caption":"Set property"},{"created":"20251114150746675","text":"!! How can I update the field value of a given tiddler inside the json data tiddler?\n\n<<ex \"\"\"[{MyJson}] \n  +[jsonset[A tiddler],[color],[#facade]]\n\"\"\">>\n\n","tags":"[[JSON Question]]","title":"Question 3","modified":"20251114154436836","caption":"Update property"},{"created":"20251114153031396","text":"!! How can I extract the list of tiddlers title present in the json data tiddler?\n\n!!! Code\n```\n<<list-links \"[{MyJson}jsonindexes[]]\">>\n```\n\n!!! Result\n<<list-links \"[{MyJson}jsonindexes[]]\">>\n","tags":"[[JSON Question]]","title":"Question 4","modified":"20251114154508871","caption":"Extract indexes"},{"created":"20251114153741728","text":"!! How can I extract the field name of a tiddler present in the json data tiddler?\n\nThe question seems unclear.  //Which// field name to you want to extract?  This will give you a list of them:\n\n```\n<<list-links \"[{MyJson}jsonindexes[A tiddler]]\">>\n```\n\n!! Result\n<<list-links \"[{MyJson}jsonindexes[A tiddler]]\">>\n","tags":"[[JSON Question]]","title":"Question 5","modified":"20251114154736851","caption":"Extract property names"},{"created":"20251114153947749","text":"!! How can I extract the field value of a tiddler present in the json data tiddler?\n\n!!! Code\n```\n[{MyJson}]\n  +[jsonget[A tiddler],[color]]\n```\n\n!!! Result\n<$let result={{{\n[{MyJson}]\n  +[jsonget[A tiddler],[color]]}}}>\n  <pre><$text text={{{ [<result>] }}} /></pre>\n</$let>\n","tags":"[[JSON Question]]","title":"Question 6","modified":"20251114161731284","caption":"Extract property value"},{"created":"20251114154818155","text":"!! How can I remove a field name and value from a tiddler present in the json data tiddler?\n\nThis one is harder.  There is a [[missing `jsondelete` operator|https://github.com/TiddlyWiki/TiddlyWiki5/pull/9390]], expected to be included in `5.4.0`\n\n!!! Code @@ -not working in 5.3.x@@\n```\n[{MyJson}] \n  +[jsondelete[A tiddler],[icon]]\n```\n\n!!! Result @@-will eventually look like@@\n\n```\n{\n    \"A tiddler\": {\n        \"color\": \"#fedcba\"\n    },\n    \"Another tiddler\": {\n        \"icon\": \"another-icon\",\n        \"color\": \"#abcdef\"\n    }\n}\n```\n\nMore options are at the [[`Delete JSON Object` thread|https://talk.tiddlywiki.org/t/14324]]","tags":"[[JSON Question]]","title":"Question 7","modified":"20251114171553516","caption":"Delete property"},{"created":"20251114155912007","text":"!! How can remove a tiddler present in the json data tiddler?\n\nSimilarly, this is not available for working with a JSON //string//.  But if you want to directly update a field in a tiddler (here the `text` field), you can use `action-setfield`, and not supply the usual `$value` parameter\n\nMore options are in the thread [[Delete JSON Object|https://talk.tiddlywiki.org/t/14324]].\n\n!!! Code\n```\n<$action-setfield $tiddler=\"MyJson\" $index=\"A tiddler\"/>\n```\n\n!!! Result \n\n''Caution: This updates the actual tiddler''.  It does not simply return a string\n\n```\n{\n    \"Another tiddler\": {\n        \"icon\": \"another-icon\",\n        \"color\": \"#abcdef\"\n    }\n}\n```","tags":"[[JSON Question]]","title":"Question 8","modified":"20251114162402420","caption":"Delete top-level property"}]