James Shelton
James Shelton

Reputation: 47

json object inside php object encode

Currently getting json results:

  {

    "post_company_success_company_address_ibfk_1": [
        "123 Main Street N\/A Cincinnati Ohio 45500"
    ]
}

The goal is include address as a separate JSON object in an array attribute of the company object.

I have tried this:

  $real= json_encode(['post_company_success_'.$randomname => {[  $result2]}], JSON_PRETTY_PRINT);

It did not work.

Upvotes: 1

Views: 56

Answers (1)

tadman
tadman

Reputation: 211740

The trick with JSON encoding is to structure your data into a singular object of some kind, typically some kind of array, and then call json_encode on it once.

For example:

json_encode([ 'key1' => $data1, 'key2' => $data2 ]);

If you concatenate multiple JSON documents together that's not valid from a JSON perspective, so any assembly you perform will need to be undone on the receiving end before processing and parsing.

There are quasi-standards like JSON Lines or Line-Delimited JSON which specify how to encode multiple documents, but these are outside of the JSON specification itself.

Upvotes: 1

Related Questions