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
} 
6
  • mmmmmm json_encode? Commented Jun 3, 2021 at 21:58
  • Using json_encode could work, that will generate a valid JSON string from an array and it's simple to use, here's a quick demo. Commented Jun 3, 2021 at 22:04
  • Does this answer your question? Returning JSON from a PHP Script Commented Jun 3, 2021 at 22:05
  • @KimHallberg No, it does not. Commented Jun 3, 2021 at 22:08
  • What about this one then? How to POST JSON Data with PHP cURL Commented Jun 3, 2021 at 22:08

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

Have you tried the json_encode() function?

1 Comment

Yes, but I lose { } or [ ] when using json_encode(), which ends up giving me a server error when making the post request.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.