I have an xml file which has several elements (3rd and 4th level <def>
as well as <ex>
and <kref>
) that may repeat 1 or more times. When I convert this to json (and evenually yaml with yq), the multiple elements are arrays and understandably the single elments are not. How can I make jq/yq consisently render an element in an array even as a single item.
For brevity, I have included below sample xml and json data with the single elements as well as the desired json output where the single elements would be rendered as an array.
I have also tried converting with cat test.xml | xq '.xdxf.lexicon.ar[].def.def.def=[.def]'
, and this seems to be starting to get me somewhere, but the resulting def array is empty. Any help on solving this much appreciated.
original xml:
<xdxf revision="034">
<lexicon>
<ar>
<k xml:lang="dyu">headword2</k>
<def freq="">
<co></co>
<def xml:lang="fr">
<def def-id="63491b7b-5d72-400c-95ee-f2e63f984832">
<gr>
<abbr></abbr>
</gr>
<co></co>
<def def-id="32dc09fd-212f-42da-9f45-7d0fa5258c49">
<deftext>
<dtrn>definition1</dtrn>
</deftext>
<ex type="exm" ex-id="dda450aa-121b-415f-92e4-63366fd78fb5">
<ex_orig></ex_orig>
<ex_tran></ex_tran>
</ex>
<etm></etm>
<categ></categ>
</def>
</def>
</def>
<sr>
<kref type="spv"></kref>
</sr>
</def>
</ar>
</lexicon>
</xdxf>
piped to jq .
{
"xdxf": {
"@revision": "034",
"lexicon": {
"ar": {
"k": {
"@xml:lang": "dyu",
"#text": "headword2"
},
"def": {
"@freq": "",
"co": null,
"def": {
"@xml:lang": "fr",
"def": {
"@def-id": "63491b7b-5d72-400c-95ee-f2e63f984832",
"gr": {
"abbr": null
},
"co": null,
"def": {
"@def-id": "32dc09fd-212f-42da-9f45-7d0fa5258c49",
"deftext": {
"dtrn": "definition1"
},
"ex": {
"@type": "exm",
"@ex-id": "dda450aa-121b-415f-92e4-63366fd78fb5",
"ex_orig": null,
"ex_tran": null
},
"etm": null,
"categ": null
}
}
},
"sr": {
"kref": {
"@type": "spv"
}
}
}
}
}
}
}
Desired output:
{
"xdxf": {
"@revision": "034",
"lexicon": {
"ar": {
"k": {
"@xml:lang": "dyu",
"#text": "headword2"
},
"def": {
"@freq": "",
"co": null,
"def": {
"@xml:lang": "fr",
"def": [
{
"@def-id": "63491b7b-5d72-400c-95ee-f2e63f984832",
"gr": {
"abbr": null
},
"co": null,
"def": [
{
"@def-id": "32dc09fd-212f-42da-9f45-7d0fa5258c49",
"deftext": {
"dtrn": "definition1"
},
"ex": [
{
"@type": "exm",
"@ex-id": "dda450aa-121b-415f-92e4-63366fd78fb5",
"ex_orig": null,
"ex_tran": null
}
],
"etm": null,
"categ": null
}
]
}
]
},
"sr": {
"kref": [
{
"@type": "spv"
}
]
}
}
}
}
}
}