0

Currently I have the below JSON as my out put from my php function (also below). However, I need the initial brackets for name list to change from [] to {}. I see this kind of output with actual APIs but I cannot figure out how to make this happen. Any suggestions? JSON Output file:

    {
    "nameList": [
            {
                "id": 1,
                "Name": "Test",
                "Amount": 0,
                "Location": "Test"
            }
    ]
}

PHP Code:

$json = json_encode(array("nameList" => array($MySQL->ReturnDataArray("*","names","WHERE Location = '". $Location ."'")), JSON_PRETTY_PRINT);
        file_put_contents('/assets/server/output.json', $json);
5
  • Why do you need this change? Commented Sep 14, 2018 at 20:39
  • A plugin that I am trying to use only grabs objects with the {} brackets. Commented Sep 14, 2018 at 20:40
  • 2
    Odd. It would seem a plugin could use valid JSON Commented Sep 14, 2018 at 20:41
  • what do you want to do if nameList has multiple objects in there Commented Sep 14, 2018 at 20:41
  • 1
    Can you show what you want the output to be like? You say you've seen it from other APIs, but that doesn't seem likely since you want invalid JSON. Commented Sep 14, 2018 at 20:52

2 Answers 2

1

You are explicitly wrapping your data in array:

$data = $MySQL->ReturnDataArray("*","names","WHERE Location = '". $Location ."'");
$json = json_encode(array("nameList" => array($data), JSON_PRETTY_PRINT); // inside []
$json = json_encode(array("nameList" => $data, JSON_PRETTY_PRINT); // without []
Sign up to request clarification or add additional context in comments.

Comments

0

Please use JSON_FORCE_OBJECT to achieve this

json_encode($foo, JSON_FORCE_OBJECT)

or in your case

json_encode($foo, JSON_FORCE_OBJECT | JSON_PRETTY_PRINT)

But listen to the comments. It is not really a good idea. An array should be an array.

But maybe you want something else.... Lets split your commands:

$step1 = $MySQL->ReturnDataArray("*","names","WHERE Location = '". $Location ."'");

$step2 = array($step1);

$step3 = array("nameList" => $step2);

$step4 = json_encode($step3, JSON_PRETTY_PRINT);

Maybe you want to remove step2!!! So try it like this:

$step1 = $MySQL->ReturnDataArray("*","names","WHERE Location = '". $Location ."'");

//$step2 = array($step1);

$step3 = array("nameList" => $step1);

$step4 = json_encode($step3, JSON_PRETTY_PRINT);

6 Comments

Using force object changes the array completely to: {"nameList":{"0":{"id":1,"Name":"Test","Amount":0,"Location":"Test"}}}, how would you get the secondary "0" out of there. Also, if an array should be an array then why do companies like rottentomato output it with {} instead of []
Yes, an Object has always an index. You want an Object? You need an index!
@DreadedSlug Or do you want to remove the array because you have always one result?
How would you remove the index or change the index?
@DreadedSlug Objects are always of the form { "key": value, "key": value, ...}. You can't have {} delimiters without key:value pairs inside.
|

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.