1

Is there an easy way to convert JSON to QBXML for import into QB Desktop? I have tried using the online converters and the results are sketchy at best, and I need something I can wrap into a web based application.

2 Answers2

0

If you're starting with something like this:

{
  "CustomerRef": {
    "FullName": "Test Customer, LLC"
  },
  "TxnDate": "2016-02-21",
  "InvoiceLineAdd": [
    {
      "ItemRef": {
        "FullName": "Test Item 1"
      },
      "Quantity": 5
    },
    {
      "ItemRef": {
        "FullName": "Test Item 2"
      },
      "Quantity": 3
    }
  ]
}

You could use a function like this:

function jsonToXML(&$root, $arr, $base = null)
{
    foreach ($arr as $key => $value)
    {
        if (is_numeric($key))
        {
            $key = $base;
        }

        if (is_array($value))
        {
            $child = $root->addChild($key);
            jsonToXML($child, $value, $key);
        }
        else
        {
            $root->addChild($key, $value);
        }
    }
}

And call it like this:

$root = new SimpleXMLElement('<InvoiceAdd/>');
jsonToXML($root, $arr);
print($root->asXML());

To get results like this:

<?xml version="1.0"?>
<InvoiceAdd>
    <CustomerRef>
        <FullName>Test Customer, LLC</FullName>
    </CustomerRef>
    <TxnDate>2016-02-21</TxnDate>
    <InvoiceLineAdd>
        <InvoiceLineAdd>
            <ItemRef>
                <FullName>Test Item 1</FullName>
            </ItemRef>
            <Quantity>5</Quantity>
        </InvoiceLineAdd>
        <InvoiceLineAdd>
            <ItemRef>
                <FullName>Test Item 2</FullName>
            </ItemRef>
            <Quantity>3</Quantity>
        </InvoiceLineAdd>
    </InvoiceLineAdd>
</InvoiceAdd>
Keith Palmer Jr.
  • 27,666
  • 16
  • 68
  • 105
  • Thanks! I'm starting with an export - which I can send you a copy of and I'm ultimately trying to import into QB as a Purchase Order with a custom field. – rgfischerjr Feb 22 '16 at 18:22
0

xmlbuilder-js for node.js can parse ordinary JavaScript objects: https://github.com/oozcitak/xmlbuilder-js

Here's a one liner:

var builder = require('xmlbuilder')

var foo = { //JSON data }

var requestXML = builder.create(JSON.parse(foo)).end();

Dillon
  • 79
  • 1
  • 4