0

I need pass on one json one element how an array of elemnts

Correct json:

{
    "issued": true,
    "customer": 451071,
    "invoice_lines": [{
        "quantity": 1,
        "concept": "Renovar Dominio - xxx.org - 1 A\u00f1o(s) (22\/03\/2018 - 21\/03\/2019) + Protecci\u00f3n de ID",
        "amount": 13.15,
        "discount": 0,
        "tax": 21,
        "retention": 0
    }, {
        "quantity": 1,
        "concept": "Renovar Dominio - xxx.net - 1 A\u00f1o(s) (22\/03\/2018 - 21\/03\/2019) + Protecci\u00f3n de ID",
        "amount": 12.73,
        "discount": 0,
        "tax": 21,
        "retention": 0
    }],
    "charges": [{
        "date": "2018-03-05",
        "amount": 74.69,
        "method": "paypal",
        "destination_account": 39720,
        "charged": true
    }],
    "date": "2018-03-05",
    "number": 4
}

Well, in my code before json_encode try several ways, but all times get incorrect json

{
    "issued": true,
    "customer": 451071,
    "invoice_lines": [
        [{
            "quantity": 1,
            "concept": "Renovar Dominio - xxx.org - 1 A\u00f1o(s) (22\/03\/2018 - 21\/03\/2019) + Protecci\u00f3n de ID",
            "amount": 13.15,
            "discount": 0,
            "tax": 21,
            "retention": 0
        }, {
            "quantity": 1,
            "concept": "Renovar Dominio - xxxx.net - 1 A\u00f1o(s) (22\/03\/2018 - 21\/03\/2019) + Protecci\u00f3n de ID",
            "amount": 12.73,
            "discount": 0,
            "tax": 21,
            "retention": 0
        }]
    ],
    "charges": [{
        "date": "2018-03-05",
        "amount": 74.69,
        "method": "paypal",
        "destination_account": 39720,
        "charged": true
    }],
    "date": "2018-03-05",
    "number": 4
}

In my php code and try others but get same problem:

$invoiceLines = array();

foreach ($whmcsLines as $whmcsLine) {
    $lines = [
        'quantity' => 1,
        'concept' => str_replace("\n","",$whmcsLine->description),
        'amount' => (double)$whmcsLine->amount,
        'discount' => 0,
        'tax' => $whmcsLine->taxed ? 21 : 0,
        'retention' => 0
    ];

    array_push($invoiceLines, $line);

}

Edited:

After process all ellemnts I execute:

$newInvoice = array(
            'issued' => true,
            'customer' => $this->customerId,
            'invoice_lines'=> array($invoiceLines),
            'charges' => array($paymentLines),
            'date' => $this->lastDate,
            'number' => $this->nextInvoice
        );

$this->someAction(json_encode($newInvoice)));
7
  • The code you've included looks correct, but you need to show us what you do with $invoiceLines afterwards. Commented Apr 17, 2018 at 19:28
  • I don't see a difference between correct and incorrect, was there a copy paste error? Commented Apr 17, 2018 at 19:35
  • 2
    @Joni There's just one extra array level around invoice_lines in the second one. I didn't see it at first either. Commented Apr 17, 2018 at 19:37
  • 3
    I'd guess it's probably just as simple as accidentally doing 'invoice_lines' => [$invoiceLines] before you JSON encode. Commented Apr 17, 2018 at 19:41
  • 1
    Please, check out the comments of @Don'tPanic. Commented Apr 17, 2018 at 19:53

1 Answer 1

2
'invoice_lines'=> array($invoiceLines),
'charges' => array($paymentLines),

This is incorrect -- it's wrapping $invoiceLines and $paymentLines (which are already arrays) in an extra array each.

What you want is simply:

'invoice_lines' => $invoiceLines,
'charges' => $paymentLines,
Sign up to request clarification or add additional context in comments.

2 Comments

The array around 'charges' seems to be wanted, because the incorrect JSON is the same as the correct JSON for this part. Right?
@Syscall If that's the case, then there's probably some other error in how $paymentLines is being generated -- it seems very unlikely that it's always supposed to be a single-element array.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.