0

I am trying to create a JSON representation of data stored in mySQL.

I am trying to document a RESTful API.

I am using PHP's json_encode() function.

I have a table that contains data such as

1) name 2) parent 3) data_type (object/array/string/number to match JSON data types) 4) value

I am trying to create a generalized function that will allow me to build these JSON strings by simply adding data to the mySQL database.

I am having problems with working with both objects and arrays though.

For instance the JSON should be:

{
"sessionToken":"","roleName":""
,"data":[
                {
                    "methodTypes":[""] , "objects":[""]
                }
            ]
}

however it is coming out as:

{
    "sessionToken":"","roleName":""
    ,"data":[
                {
                "methodTypes":[""]
            }
            ,{
                "objects":[""]
                }
            ]
}

this is indicating to me for some reason my code is adding an object for both methodType and objects, where as it should just be within the single object.

I am trying to first create an array containing methodTypes and objects. Then I create an object in the format of $objects->$A, and I make this equal the array created in the first step. Then I add in to the primary data array for the JSON generation.

I have been searching for examples that show usage examples of JSON when both arrays and objects are required in the same JSON without success.

Any pointers in the correct direction would be greatly appreciated.

UPDATE #1:

var_dump of the array that is being fed to json_encode() is:

array(3) { ["sessionToken"]=> string(0) "" ["roleName"]=> string(0) "" ["data"]=>     array(2) { [0]=> array(1) { ["methodTypes"]=> array(1) { [0]=> string(0) "" } } [1]=>     array(1) { ["objects"]=> array(1) { [0]=> string(0) "" } } } }

where as if I take a known good JSON and do a json_decode() then the var_dump looks like:

object(stdClass)#3 (3) { ["sessionToken"]=> string(0) "" ["roleName"]=> string(0) "" ["data"]=> array(1) { [0]=> object(stdClass)#4 (2) { ["methodTypes"]=> array(1) { [0]=> string(0) "" } ["objects"]=> array(1) { [0]=> string(0) "" } } } }

or

array(3) { ["sessionToken"]=> string(0) "" ["roleName"]=> string(0) "" ["data"]=> array(1) { [0]=> array(2) { ["methodTypes"]=> array(1) { [0]=> string(0) "" } ["objects"]=> array(1) { [0]=> string(0) "" } } } }

if I set it to TRUE to return array instead of object.

5
  • Can you post a var_dump of the array you're using with json_encode? I think you might have your data structured wrong on that end Commented Oct 27, 2013 at 19:15
  • Machavity thanks for responding. I've updated the post to show the var_dump info. Can I add anything else to help make the question clearer? Commented Oct 27, 2013 at 19:36
  • try just using php arrays since they can also be associative and json_encode will adjust to javascript object if needed. For example $arr['foo']=array('bar', 'gggg') will convert to {"foo":["bar", "gggg"]} Commented Oct 27, 2013 at 19:39
  • here's a very helpful php sandbox can test php code in browser with sandbox.onlinephpfunctions.com Commented Oct 27, 2013 at 19:43
  • Charliefl that would be great if that would work, I don't see how to do that though. Can you provide a working example that will generate JSON equal to the first code black in this question posting? I tried `$test = array('sessionToken'=>'' ,'roleName'=>'' ,'data' => array('methodTypes' => array() ,'objects' => array() ) ) ; $test_json = json_encode($test) ; echo $test_json without luck Commented Oct 27, 2013 at 19:49

1 Answer 1

1

Edited to give the desired output

$data = ['sessionToken' => '',
    'roleName' => '',
    'data' => [['methodTypes' => [''], 'objects' => ['']]]
];

And it yields. This works because I wrapped the associative array in a non-associative one

{"sessionToken":"","roleName":"","data":[{"methodTypes":[""],"objects":[""]}]}

EDIT

Some more info on json_encode Javascript arrays contain only numeric keys. Period. If you have what looks like an associative array, it's really a JS object (which can be referenced using brackets, i.e. var['name'] and var.name are equivalent).

Since json_encode can't create an associative array in JSON, it converts associative PHP arrays into objects instead. So you can see in my example above, I got an object back instead of an array, except where I has not specified keys.

echo json_encode(['name' => 'value']);

Yields

{"name":"value"}

While

echo json_encode(['value']);

Yields

["value"]
Sign up to request clarification or add additional context in comments.

2 Comments

Machavity the JSON that I need to create needs to mathch: {"sessionToken":"","roleName":"","data":[{"methodTypes":[""] ,"objects":[""] }]} In non programming code the general format would be something like data.[].{}.methodTypes where the square brackets[] indicate a JSON array and the curly brackets{} indicate a JSON object. I believe the API is this way because you can send multiple objects.
Edited my response. Will give you the string you want

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.