0

below my json :

  "temperature": {
    "level": 8,
    "format": function (value) {
                            return value + ' (C°)';
                            },
    "minimum": null,
    ...
    ...
    }

My goal is to write key format, I have try this but without much conviction...

package require rl_json
namespace import rl_json::json

set rj "{}"
set s1 {function (value) {
            return value + ' (C°)';
        }
        }


json set rj temperature [json object [list level {number 8} format [json template {{"~L:s1"}}] minimum {null}]]

Error parsing JSON value: Expecting : after object key at offset 8

Mkn
  • 498
  • 1
  • 3
  • 13
  • 1
    JSON can not contain functions. See also: [Is it valid to define functions in JSON results?](https://stackoverflow.com/questions/2001449/is-it-valid-to-define-functions-in-json-results) – Schelte Bron Apr 23 '22 at 10:59
  • @SchelteBron , It is not standard... but in my case I have JSON files that contain functions. Is there a workaround with `rl_json` package ? – Mkn Apr 23 '22 at 11:08
  • You are trying to create JSON containing a function. That is not valid (or as you call it, standard) JSON. It is not supported by rl_json. – Schelte Bron Apr 23 '22 at 11:13
  • A workaround could be to insert some special pattern in the JSON string, which you later replace using `string map`. But from that point on, `rl_json` will no longer be able to work with the value. – Schelte Bron Apr 23 '22 at 11:16
  • @SchelteBron, ok I wanted to avoid using that... but if it's not supported, I will use `string map` – Mkn Apr 23 '22 at 11:21

1 Answers1

0

What you are trying to produce is not (partial) JSON, as defined, but rather general Javascript. As such, you cannot reasonably expect JSON-specific tooling (such as rl_json) to work with it. It is usually better to keep your JSON and your Javascript separated, with the data in JSON (including any dynamic generation) and your Javascript as a static artefact (or generated from something like Typescript). Mixing code and dynamic generation requires great care to get right; it is usually better to try to write things so you don't need to be that careful!

If you must do this defintely-not-advised thing, use something like subst or format to inject the variable parts (which you can generate with rl_json) in the overall string. No code for that from me: my advice is don't do that.

Donal Fellows
  • 133,037
  • 18
  • 149
  • 215
  • ok If I can't how do you do ? `function` or `variable` are not supported with `rl_json` ? If I understand `subst` , `string map` or `format` is for me the only solution – Mkn Apr 24 '22 at 13:18
  • You. Can't. Put. Code. In. There. This is difficult because you should not do this. (Put in the name of a function — as a string — to call that is defined elsewhere.) – Donal Fellows Apr 25 '22 at 08:12
  • Sorry English is not my first language. You’re right`,` punctuation is important to make that clear`.` That said, thanks for your comment`...` – Mkn Apr 25 '22 at 09:45