JSON building complex using php

I need to create JSON from PHP. The JSON structure is less than trivial:

{ 

    "new" : {
        "checkpoints" : 
            [
                { 
                    "id" : "businessidea",
                    "name" : "business idea",
                    "purpose" : "Form a business idea", 
                    "customer" : 
                        { "questions" : 
                            [

                                { "questionid" : "id1", "questiontitle": "Evaluate size of the market, likely growth vectors and estimate addressable size.", "answers" :
                                  [
                                      {"answertext" : "an answer here", "answerlink": "", "answers": [
                                          {"answertext": "answer text here", "answerlink": "http://google.com"},
                                          {"answertext": "answer text here", "answerlink": "http://google.com"},
                                          {"answertext": "answer text here", "answerlink": "http://google.com"},
                                          {"answertext": "answer text here", "answerlink": "http://google.com"}
                                      ]},
                                      {"answertext" : "an answer here", "answerlink": "", "answers": [
                                          {"answertext": "answer text here", "answerlink": "http://google.com"},
                                          {"answertext": "answer text here", "answerlink": "http://google.com"},
                                          {"answertext": "answer text here", "answerlink": "http://google.com"},
                                          {"answertext": "answer text here", "answerlink": "http://google.com"}
                                      ]},
                                      {"answertext" : "an answer here", "answerlink": "", "answers": [
                                          {"answertext": "answer text here", "answerlink": "http://google.com"},
                                          {"answertext": "answer text here", "answerlink": "http://google.com"},
                                          {"answertext": "answer text here", "answerlink": "http://google.com"},
                                          {"answertext": "answer text here", "answerlink": "http://google.com"}
                                      ]}
                                  ]
                                },
                                { "questionid" : "id2","questiontitle": "Define the needs of the customers and the value we will deliver to the customers - customer pain and our solution", "answers" :
                                  [
                                      {"answertext" : "an answer here", "answerlink": "", "answers": [
                                          {"answertext": "answer text here", "answerlink": "http://google.com"},
                                          {"answertext": "answer text here", "answerlink": "http://google.com"},
                                          {"answertext": "answer text here", "answerlink": "http://google.com"},
                                          {"answertext": "answer text here", "answerlink": "http://google.com"}
                                      ]},
                                      {"answertext" : "an answer here", "answerlink": "", "answers": [
                                          {"answertext": "answer text here", "answerlink": "http://google.com"},
                                          {"answertext": "answer text here", "answerlink": "http://google.com"},
                                          {"answertext": "answer text here", "answerlink": "http://google.com"},
                                          {"answertext": "answer text here", "answerlink": "http://google.com"}
                                      ]},
                                      {"answertext" : "an answer here", "answerlink": "", "answers": [
                                          {"answertext": "answer text here", "answerlink": "http://google.com"},
                                          {"answertext": "answer text here", "answerlink": "http://google.com"},
                                          {"answertext": "answer text here", "answerlink": "http://google.com"},
                                          {"answertext": "answer text here", "answerlink": "http://google.com"}
                                      ]}
                                  ]
                                 },
                                { "questionid" : "id3","questiontitle": "Define the competitor landscape" , "answers" :
                                  [
                                      {"answertext" : "an answer here", "answerlink": "", "answers": [
                                          {"answertext": "answer text here", "answerlink": "http://google.com"},
                                          {"answertext": "answer text here", "answerlink": "http://google.com"},
                                          {"answertext": "answer text here", "answerlink": "http://google.com"},
                                          {"answertext": "answer text here", "answerlink": "http://google.com"}
                                      ]},
                                      {"answertext" : "an answer here", "answerlink": "", "answers": [
                                          {"answertext": "answer text here", "answerlink": "http://google.com"},
                                          {"answertext": "answer text here", "answerlink": "http://google.com"},
                                          {"answertext": "answer text here", "answerlink": "http://google.com"},
                                          {"answertext": "answer text here", "answerlink": "http://google.com"}
                                      ]},
                                      {"answertext" : "an answer here", "answerlink": "", "answers": [
                                          {"answertext": "answer text here", "answerlink": "http://google.com"},
                                          {"answertext": "answer text here", "answerlink": "http://google.com"},
                                          {"answertext": "answer text here", "answerlink": "http://google.com"},
                                          {"answertext": "answer text here", "answerlink": "http://google.com"}
                                      ]}
                                  ]}
                            ]
                        },

What is the best way to achieve this? Am I going the way of building this json using a lot of string concatenation or is it possible to use the PHP built in JSON tools?

+3
source share
5 answers

I am following the path of building this json using lots of string concatenation

No. Never create JSON by compiling strings.

or is it possible to use PHP built into JSON tools?

Yes.

Build a data structure in PHP. The specifics of how you do this depends on where you get the data from in the first place.

When it is completed, go through json_encode.

+6
source

JSON!

( stdClass) json_encode() it.

+2

Use an array to structure the data, and then use json_encode to turn it into JSON. JSON Encode Documentation

+2
source
<?php echo json_encode($yourArray); ?>
+1
source

Use json_decode, add trueas the second parameter, if you want it to be like an array.

0
source

All Articles