3

I need to build some JSON out from PHP. The structure of the JSON 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? Do I go down the route of building this json by using lots of string concatenation or is it feasible to use PHP's built in JSON tools?

5 Answers 5

6

Do I go down the route of building this json by using lots of string concatenation

No. Never build JSON by mashing together strings.

or is it feasible to use PHP's built in JSON tools?

Yes.

Build a data structure in PHP. There specifics of how you do that depend on where you are getting the data from in the first place.

When it is complete, pass it through json_encode.

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

3 Comments

Using a string to create a JSON is not the best way, you will force PHP to eval the string to knows the type of data and that is a key and that is data. Using an array you not need to do the eval, because internally the array knows the data type of values
Ok, How do i build a data structure in php? I'm getting the data from a database call.
By creating and assigning Arrays.
2

You never use string functions to build JSON!

Create an array (or stdClass object) and json_encode() it.

2 Comments

There is simply no acceptable reason to do it unless you are writing a JSON serializer for a language where no good one exists.
Often say never. Some approaches to a problem are just wrong. (And edge cases such as "We need it yesterday, and don't have time to upgrade PHP to something remotely modern" should be screamed about and fixed properly as soon as possible with the hack being recognised as a hack).
2

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

Comments

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

Comments

0

Use json_decode, add true as the 2nd parameter if you want it as an array.

Comments

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.