Re: remove specified key from data container

"'Nick Anderson' via help-cfengine" <[email protected]>
Newsgroups gmane.comp.sysutils.cfengine.general
Message-ID <[email protected]>
      Hi all, I like data containers with json. I have a simple
      data container

      '{ "name1": "aaaa", "name2": "bbbb", "name3": "cccc" }'

      and I don't know how to remove the key 'name2' from the data
      container.

      I tried a difference function but its returns only slist,
      not data container. What is the best practice? Is posible
      using json_pipe, how?

      Thanks Michal Svamberg

Hi Michal,

I can think of a couple ways of doing it. The first thing that came to
mind was leveraging a classic array. This is a pattern that I have used
for a long time. I still find it useful quite often. Another method that
came to mind was to leverage `jq' via `mapdata()' using the `json_pipe'
interpretation as you noted.

Here is an example:

,----
| bundle agent __main__
| {
|   vars:
|       "original" data => '{ "name1": "aaaa", "name2": "bbbb", "name3": "cccc" }';
| 
|       # Method 0, build up a classic array using iteration
|       "_original_keys" slist => getindices( original );
|       "_a[$(_original_keys)]"
|         string => "$(original[$(_original_keys)])",
|         unless => strcmp( "name2", "$(_original_keys)" );
|       "method_0" data => mergedata( "_a" );
| 
|       # Method 1, jq via json_pipe
|       # mapdata returns a json array, so we use parsejson(nth(0)) to pick out the dict
|       "_method_1_json_array"
|         data => mapdata( "json_pipe",
|                          `$(def.jq) 'del(.name2)'`,
|                          original);
|       "method_1" data => mergedata( "_method_1_json_array[0]" );
| }
`----
Listing 1: Example Policy

,----
| # cf-agent --no-lock --log-level info --show-evaluated-vars=default:main\\. --file /tmp/example.cf
| Variable name                            Variable value                                               Meta tags                                Comment                                 
| default:main._a[name1]                   aaaa                                                         source=promise                                                                   
| default:main._a[name3]                   cccc                                                         source=promise                                                                   
| default:main._method_1_json_array        [{"name1":"aaaa","name3":"cccc"}]                            source=promise                                                                   
| default:main._original_keys               {"name1","name2","name3"}                                   source=promise                                                                   
| default:main.method_0                    {"name1":"aaaa","name3":"cccc"}                              source=promise                                                                   
| default:main.method_1                    {"name1":"aaaa","name3":"cccc"}                              source=promise                                                                   
| default:main.original                    {"name1":"aaaa","name2":"bbbb","name3":"cccc"}               source=promise                                                                   
`----


I don't know that I have any firm position on what is /best/. I think
both methodologies have their own merits. The classic array generator
works anywhere CFEngine works. `mapdata()' using the `json_pipe'
interpretationrequires some external utility like jq which may not be
universally available.

Hope this helps ...

Don't forget to check out our ["The Agent is In"] series on YouTube.
Smash that like button, [subscribe], ring that bell so you get notified
of anything new, become a member ... oh, well okay we don't have YouTube
memberships yet I guess.


["The Agent is In"]
<https://www.youtube.com/watch?v=YYS1CUggomU&list=PLh71Vl9YjMaji_jnHFZ3k4mDbyOCE3A2x>

[subscribe] <https://youtube.com/cfengine/?sub_confirmation=1>

-- 
You received this message because you are subscribed to the Google Groups "help-cfengine" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
To view this discussion on the web visit https://groups.google.com/d/msgid/help-cfengine/87sfx39oks.fsf%40northern.tech.

-- 
Nick Anderson | Doer of Things | (+1) 785-550-1767 | https://northern.tech

-- 
You received this message because you are subscribed to the Google Groups "help-cfengine" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
To view this discussion on the web visit https://groups.google.com/d/msgid/help-cfengine/87sfx39oks.fsf%40northern.tech.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.