sjsonnet icon indicating copy to clipboard operation
sjsonnet copied to clipboard

null Safe select operator, aka. Optional_chaining

Open He-Pin opened this issue 8 months ago • 1 comments

refs: https://github.com/google/jsonnet/issues/930 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining#optional_chaining_with_expressions

In JSONPath, we can select deeply, even it may be null.

  maybeNull: someObject?.innerObject?.whatIWant

I think we can support this with a ?. instead of ..

I have implemented it with :

      eval(
        """
          |local obj = {a: 1};
          |local obj2 = null;
          |obj2?.a
          |
          |""".stripMargin) ==> ujson.Null

or maybe just use std.get

This project is using this syntax https://github.com/jam01/xtrasonnet

He-Pin avatar Apr 27 '25 08:04 He-Pin