Removing field from object

2,102 views
Skip to first unread message

siark...@google.com

unread,
Jun 21, 2018, 4:09:51 AM6/21/18
to Jsonnet
Hi,

I'm really new to jsonnet so sorry for beginner question.
I want to use jsonnet for managing Grafana Dashboards for Kubernetes based on https://github.com/kubernetes-monitoring/kubernetes-mixin
But I got to the problem that some dashboards do not work for me, so I would like to remove them in jsonnet. 
It should be simple, right? 

Just import "kubernetes-mixin/mixin.libsonnet", got under field "grafanaDashboards" and delete key.

I didn't find information about how to do deletion beside https://github.com/google/jsonnet/issues/312
But I don't have enough knowledge to understand how to use it. e. g. comma in solution causes `STATIC ERROR: mixin.libsonnet:8:57: expected for, if or "}" after for clause, got: ","`

Can someone give me some hints how to handle that?

Dave Cunningham

unread,
Jun 21, 2018, 10:47:03 PM6/21/18
to Jsonnet
There is a proposal to add the ability to delete a field in a mixin, but it's not implemented yet. However there are a few ways of achieving something similar:
- Override the field but make it hidden with `{ foo:: super.foo }`
- Build a new object via object composition with everything except the field(s) you don't want
- change the field value to null if the consumer accepts that and ignores it (often the case)

Colin Spargo

unread,
Jun 26, 2018, 7:32:44 PM6/26/18
to Jsonnet
I've been using the stdlib mergePatch function to achieve this

{
  a: {
    b: 'c',
    d: 'e',
  },
  f: {
    g: ['h', 'i'],
  },
} + {
  a: std.mergePatch(super.a, {
    d: null, // replaces 'a' with 'd' removed
  }),
}

Muhammad Enzam Hossain

unread,
May 23, 2022, 5:52:22 PM5/23/22
to Jsonnet
I am using `std.prune` to achieve that, but note that it will remove all null value fields.

For example,

std.prune({
  a: "okay",
  b: "will get removed",
  c: null, // will get removed
  d: {
    e: "okay",
    f: "will get removed",
    g: null, // will get removed
  },
} + {
  // Remove the following fields
  b: null,
  d+: { f: null },
})

will result
{
  "a": "okay",
  "d": {
    "e": "okay"
  }
}
Reply all
Reply to author
Forward
0 new messages