1

I am trying to create dynamically a JSON from a MySQL, which seems fairly easy with this piece of code I found in this thread Create nested json object using php mysql. However, I want to add before and after the $json_response some piece of JSON code.

The main code

    $result = mysql_query("SELECT * FROM Places "); 
$json_response = array(); //Create an array
while ($row = mysql_fetch_array($result))
{
    $row_array = array();
    $row_array['title'] = $row['title'];        
    $row_array['image_url'] = $row['image_url'];
    $row_array['subtitle'] = $row['subtitle'];      
    $row_array['buttons'] = array();
    $id = $row['id'];


    $option_qry = mysql_query("SELECT * FROM Places where id=$id");
    while ($opt_fet = mysql_fetch_array($option_qry))
    {
        $row_array['buttons'][] = array(
            'type' => $opt_fet['type'],
            'caption' => $opt_fet['caption'],
            'url' => $opt_fet['url'],
        );

    }
    array_push($json_response, $row_array); //push the values in the array
}

echo json_encode($json_response,  JSON_PRETTY_PRINT);

Produces this JSON

    [
        {
            "title": "Name of the place",
            "image_url": "image.jpg",
            "subtitle":Additional info",
            "buttons": [
                {
                    "type": "'url'",
                    "caption": "More Info",
                    "url": "https://some link "
                }
            ]
        },
        {
            "title": "Name of the place 2",
            "image_url": "image2.jpg",
            "subtitle":Additional info2",
            "buttons": [
                {
                    "type": "'url'",
                    "caption": "More Info",
                    "url": "https://some link 2"
                }
            ]
        }
    ] 

I have somehow to add the following code before the already created JSON

{
  "version": "v2",
  "content": {
    "messages": [
      {
        "type": "cards",
        "elements":

And this code in the end

      }
    ]
  }
}

2 Answers 2

3

Pretty simple:

$final_json = [
    "version" => "v2",
    "content" => [
        "messages" => [[
            "type" => "cards",
            "elements" => $json_response
        ]]
    ]
];

echo json_encode($final_json, JSON_PRETTY_PRINT);

Personally, I'd rename $json_response to $messages for clarity purposes.

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

7 Comments

Cool, and how would be with adding the code in the end? Or it add it automatically
Yeah I have noticed after I wrote my comment :)
It throws an error --- syntax error, unexpected ':', expecting ']' --- that's on the line "version": "v2",
@lStoilov Sorry, fixed (Array syntax in PHP is => not :).
@FrankerZ, thanks... I just noticed that the { "type": "cards", "elements": is missing :) Can you help adding it too.
|
1

While declaring array $json_response = array(); you can actually prepare it with your default values require like

$stdObj=new \stdClass();
$stdObj->version="V2";
$stdObj->content=(object)["messages"=>(object)["type"=>'cards','elements'=>$row_array['buttons']]];

echo json_encode($stdObj, JSON_PRETTY_PRINT);

4 Comments

Thanks, there is an error somewhere in the third line - it says: syntax error, unexpected ';', expecting ']'
Updated the answer ] was missing
Why convert everything from an array, back to an object?
We can achieve the result @FrankerZ by the methoed you have specified, but this is also one possible way.

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.