Re: [ansible-project] How to split a json key containing a colon into 2 keys

2020-02-11 Thread jean-christophe manciot
I'm impressed. It works perfectly. Only the first key is split as expected..

On Friday, February 7, 2020 at 7:12:40 PM UTC+1, Vladimir Botka wrote:
>
> On Fri, 7 Feb 2020 08:14:56 -0800 (PST) 
> jean-christophe manciot > wrote: 
>
> > I receive some json data which *sometimes* start with the first key 
> within 
> > "json_data" variable containing a ':', such as: 
> > "json_data": { 
> > "key1:key2": { 
> > "key3": [ 
> > "value31", 
> > "value32", 
> > "..." 
> > ] 
> > } 
> > } 
> > 
> > The goal is to split "key1:key2" into 2 keys so that we end up with: 
> > "json_data": { 
> > "key1": { 
> > "key2": { 
> > "key3": [ 
> > "value31", 
> > "value32", 
> > "..." 
> > ] 
> > } 
> > } 
> > } 
>
>
> Try this 
>
>- set_fact: 
> json_data1: "{{ json_data1|default({})|combine(my_key) }}" 
>   vars: 
> my_keys: "{{ item.key.split(':') }}" 
> my_keys_length: "{{ my_keys|length == 1 }}" 
> my_key: "{{ my_keys_length| 
> ternary({my_keys[0]: item.value}, 
> {my_keys[0]: 
> {my_keys[1:]|join(':')|default('NA'): 
>  item.value}}) }}" 
>   loop: "{{ json_data|dict2items }}" 
>
> HTH, 
>
> -vlado 
>

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to ansible-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/1f82855d-3090-4502-87f1-d3a6aec28aa6%40googlegroups.com.


Re: [ansible-project] How to split a json key containing a colon into 2 keys

2020-02-07 Thread Vladimir Botka
On Fri, 7 Feb 2020 08:14:56 -0800 (PST)
jean-christophe manciot  wrote:

> I receive some json data which *sometimes* start with the first key within 
> "json_data" variable containing a ':', such as:
> "json_data": {
> "key1:key2": {
> "key3": [
> "value31",
> "value32",
> "..."
> ]
> }
> }
> 
> The goal is to split "key1:key2" into 2 keys so that we end up with:
> "json_data": {
> "key1": {
> "key2": {
> "key3": [
> "value31",
> "value32",
> "..."
> ]
> }
> }
> }


Try this

   - set_fact:
json_data1: "{{ json_data1|default({})|combine(my_key) }}"
  vars:
my_keys: "{{ item.key.split(':') }}"
my_keys_length: "{{ my_keys|length == 1 }}"
my_key: "{{ my_keys_length|
ternary({my_keys[0]: item.value},
{my_keys[0]: {my_keys[1:]|join(':')|default('NA'):
 item.value}}) }}"
  loop: "{{ json_data|dict2items }}"

HTH,

-vlado

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to ansible-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/20200207191221.653086f7%40gmail.com.


pgpAU84ovpxPz.pgp
Description: OpenPGP digital signature


[ansible-project] How to split a json key containing a colon into 2 keys

2020-02-07 Thread jean-christophe manciot
I receive some json data which *sometimes* start with the first key within 
"json_data" variable containing a ':', such as:
"json_data": {
"key1:key2": {
"key3": [
"value31",
"value32",
"..."
]
}
}

The goal is to split "key1:key2" into 2 keys so that we end up with:
"json_data": {
"key1": {
"key2": {
"key3": [
"value31",
"value32",
"..."
]
}
}
}

The key names are completely variable so I also have no idea about how to 
implement the play condition so that this transformation is done only when 
the first key within "json_data" contains a single colon. 

I am aware that a colon should not be part of any key, but I have no 
control over that.

Any suggestion?

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to ansible-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/1ec78535-917f-4758-b35b-a598db163d9b%40googlegroups.com.