Unable to convert JSON to XML with attribute and text content

Describe the problem/error/question

I’m not able to figure out how to do a conversion from JSON to XML with attributes and text nodes.

I have this JSON:

{
  "InvoicedQuantity": {
    "#text": "1",
    "@unitCode": "ks"
  }
}

and would like to convert it to

<InvoicedQuantity unitCode="ks">1</InvoicedQuantity>

I tried using different prefixes and configuring Attribute Key and Character Key in the XML node without success. Test results below.

What is the error message?

I tried several configurations, but result was either incorrect or I got an error. Here is what I tried:

Using “_” for text and configuring Character Key to “_”:

JSON:

{
  "InvoicedQuantity": {
    "_text": "1"
  }
}

XML:

<InvoicedQuantity><_text>1</_text></InvoicedQuantity>

Using “#” for text and configuring Character Key to “#”:

{
  "InvoicedQuantity": {
    "#text": "1"
  }
}

Results in error: The root node has no parent. Use doc() if you need to get the document object.

This error is very misleading. I spent two hours until I found out it is actually a “#” prefix and has nothing to do with the root.

Using @ for attribute and configuring Attribute Key to “@”:

{
  "InvoicedQuantity": {
    "@unitCode": "ks"
  }
}

Results in error: Invalid character in name

Using $ for attribute and configuring Attribute Key to “$”:

{
  "InvoicedQuantity": {
    "$unitCode": "ks"
  }
}

Results in error: Invalid character in name

Please share your workflow

Information on your n8n setup

  • n8n version: 1.110.1
  • Running n8n via (Docker, npm, n8n cloud, desktop app): n8n cloud

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.