0

I have the following JSON that works on Postman when performing a POST request:

'{
    "bar1": "sample",
    "bar2": [
        {
            "bar1": "sample",
            "bar2": "sample",
            "bar3": "111111111",
            "bar4": "sample",
            "bar5": {
                "bar6": "varr",
                "bar7": [
                    -70.11111111111111,
                    -33.11111111111111
                ]
            }
        }
    ],
    "bar8": 11111
}'

However on my backend I have an Array that I need to shape into that JSON format. The way I am building my array is as follow:

$myArray =array(
                "bar1" => "sample",
                "bar2" => (object) array(
                    "bar1" => "sample",
                    "bar2" => "sample",
                    "bar3" => "111111111",
                    "bar4"=> "sample",
                    "bar5" => (object)[
                        "bar6" => "varr",
                        "bar7" => array(
                            -70.111111111111111,
                            -33.111111111111111
                        )]
                    
            ),
                        "bar8" => 11111
                   ) ;

When it comes to send the request through cURL I get "Internal server error" as response. If I were to send the JSON raw string, I would get the correct response. What would be the best way to convert my Array into the JSON I need?

json_encode() gives me the following JSON, which resolves into a server error when making request.

{
 "bar1": "sample",
 "bar2": {
  "bar1": "sample",
  "bar2": "sample",
  "bar3": "sample",
  "bar4": "sample",
  "bar5": {
   "bar6": "varr",
   "bar7": [
    -70.111111111111,
    -33.111111111111
   ]
  }
 },
 "bar8": 11111
} 
Gamoxion
  • 159
  • 1
  • 10
  • mmmmmm `json_encode`? – Alberto Sinigaglia Jun 03 '21 at 21:58
  • Using [`json_encode`](https://www.php.net/manual/en/function.json-encode.php) could work, that will generate a valid JSON string from an array and it's simple to use, here's a quick [demo](https://3v4l.org/4qNh6). – Kim Hallberg Jun 03 '21 at 22:04
  • Does this answer your question? [Returning JSON from a PHP Script](https://stackoverflow.com/questions/4064444/returning-json-from-a-php-script) – Kim Hallberg Jun 03 '21 at 22:05
  • @KimHallberg No, it does not. – Gamoxion Jun 03 '21 at 22:08
  • What about this one then? [How to POST JSON Data with PHP cURL](https://stackoverflow.com/questions/11079135/how-to-post-json-data-with-php-curl) – Kim Hallberg Jun 03 '21 at 22:08
  • @KimHallberg neither, my problem is that my Array when json_encode it doesn't follow the format of the original JSON. If I try the converted json on Postman it throws me the same server error. – Gamoxion Jun 03 '21 at 22:11

2 Answers2

2

To make json_encode generate an array of json objects, the key bar2 needs to contain an array of arrays, i.e.

$myArray = array(
    "bar2" => array(
        array(
            "hello" => "world"
        ),
    ),
);

Updating your original array to the following format will generate a json object with bar2 returning an array of objects.

$myArray = array(
    "bar1" => "sample",
    "bar2" => array(
        array(
            "bar1" => "sample",
            "bar2" => "sample",
            "bar3" => "111111111",
            "bar4" => "sample",
            "bar5" => [
                "bar6" => "varr",
                "bar7" => array(
                    -70.111111111111111,
                    -33.111111111111111
                )
            ]
        )
    ),
    "bar8" => 11111
);

The following array will generate the following json.

{
  "bar1": "sample",
  "bar2": [
    {
      "bar1": "sample",
      "bar2": "sample",
      "bar3": "111111111",
      "bar4": "sample",
      "bar5": {
        "bar6": "varr",
        "bar7": [
          -70.11111111111111,
          -33.111111111111114
        ]
      }
    }
  ],
  "bar8": 11111
}

Demo source.

Kim Hallberg
  • 1,165
  • 1
  • 9
  • 18
0

Have you tried the json_encode() function?

PaulProgrammer
  • 16,175
  • 4
  • 39
  • 56
  • Yes, but I lose { } or [ ] when using json_encode(), which ends up giving me a server error when making the post request. – Gamoxion Jun 03 '21 at 21:59