ok I’ve been in contact with chatgpt and gemmini on this and they are really out to lunch a lot of the time.
I am using https://github.com/mikefarah/yq
I am reading from xml and need to ensure single items are arrays, and I have been successful at that with several objects including:
.xdxf.lexicon.ar[].def[].def[].def[0].def[].ex |= ([] + .)
I can check the data with (note the 3rd def[0])
.xdxf.lexicon.ar[].def[].def[].def[0].def[]
deftext: district
sr:
kref: "null"
categ: "null"
ex: []
deftext: revue
ex:
- +@type: exm
+@author: ""
+@source: ""
ex_orig: kɔrɔman
ex_tran: ancien
that shows both added empty arrays and existing arrays. But the problem is some of the items in ex[] are mandatory, so I want to populate ex[] with +@type: exm to get me started.
I have tried many syntax variations each with //, select and unique. Either the comands hang or are not returning any or bad data. I don’t want to have any ex: []
as below. I want to have at minimum:
ex:
- +@type: exm
Samples of what i have tried:
.xdxf.lexicon.ar[].def[].def[].def[0].def[].ex = (.xdxf.lexicon.ar[].def[].def[].def[0].def[].ex + [{"+@type": "exm"}] | unique)
.xdxf.lexicon.ar[].def[].def[].def[0].def[].ex // [] | .xdxf.lexicon.ar[].def[].def[].def[0].def[].ex += [{"@type":"exm"}]
.xdxf.lexicon.ar[].def[].def[].def[0].def[].ex // .xdxf.lexicon.ar[].def[].def[].def[0].def[].ex += [{"@type":"exm"}]
.xdxf.lexicon.ar[].def[].def[].def[0].def[] | select(.key == "ex" and . == []) | . |= { ex: [{"@type":"exm"}] }