REST based web services the server returns more arrays than expected





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1















I am building a REST based web services, in the server, I have a service called display_all_products the code is like so:



case 'display_all_products':
$KEY = filter_input(INPUT_GET, 'api_key');
try {
$token = JWT::decode($KEY, $secret);
$jsonProducts = getProducts();
$products = json_decode($jsonProducts, TRUE);
} catch (Exception $ex) {
return -1;
}
if($products != NULL){
echo $jsonProducts;
}else{
echo "error";
}


Function getProducts() will get all the products information from database and return a json string. When I tested the function, I got a json string like this:



[{"product_id":1,"0":1,"product_name":"Chocolate","1":"Chocolate","price":3,"2":3,"description":"Dark chocolate","3":"Dark chocolate"},{"product_id":2,"0":2,"product_name":"Toilet tissue","1":"Toilet tissue","price":10,"2":10,"description":"Nature and Soft","3":"Nature and Soft"},{"product_id":3,"0":3,"product_name":"Rice","1":"Rice","price":5,"2":5,"description":"Basmati Rice","3":"Basmati Rice"},{"product_id":4,"0":4,"product_name":"Fruit Loaf","1":"Fruit Loaf","price":5,"2":5,"description":"Rich Fruit Bread","3":"Rich Fruit Bread"},{"product_id":5,"0":5,"product_name":"Raspberry Jam","1":"Raspberry Jam","price":6,"2":6,"description":"Full of Flavour","3":"Full of Flavour"}]


It looks like a json string, nothing wrong with it. And also I can get it from the client which means I can do the json_decode in the code I provided above.



The problem is I can not do the json_decode in the client. When it comes to the client, I am getting a json string from the server like this:



[{"product_id":1,"0":1,"product_name":"Chocolate","1":"Chocolate","price":3,"2":3,"description":"Dark chocolate","3":"Dark chocolate"},{"product_id":2,"0":2,"product_name":"Toilet tissue","1":"Toilet tissue","price":10,"2":10,"description":"Nature and Soft","3":"Nature and Soft"},{"product_id":3,"0":3,"product_name":"Rice","1":"Rice","price":5,"2":5,"description":"Basmati Rice","3":"Basmati Rice"},{"product_id":4,"0":4,"product_name":"Fruit Loaf","1":"Fruit Loaf","price":5,"2":5,"description":"Rich Fruit Bread","3":"Rich Fruit Bread"},{"product_id":5,"0":5,"product_name":"Raspberry Jam","1":"Raspberry Jam","price":6,"2":6,"description":"Full of Flavour","3":"Full of Flavour"}]


Both of the json string are very similar but at the very end of the second json string has two more in there. I did not encode twice. I think that is the reason why I can not decode it but where are the two come from and how to get rid of them.



Here is my code form the client:



$keyReq = "?api_key=" . $api_key . "&service=display_all_products";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $basicUrl . $keyReq);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Accept: application/json"));
$jsonProducts = curl_exec($ch);

curl_close($ch);
$products = json_decode($jsonProducts, TRUE);









share|improve this question























  • Got any break statements in that switch? If I had to guess, I'd say some other service calls are executing due to case fallthrough

    – Phil
    Nov 21 '18 at 23:42













  • Yes,there are five services in the server so I have five break statements.

    – BoBOBO
    Nov 22 '18 at 0:32











  • OK,I see what you mean.

    – BoBOBO
    Nov 22 '18 at 0:34











  • Was that the problem then?

    – Phil
    Nov 22 '18 at 1:25











  • There are two more services below that, the two might indicate two arrays from the them. The problem might be case fallthrough but I only want to call the display_all_products service and return only one json string.

    – BoBOBO
    Nov 22 '18 at 9:22


















1















I am building a REST based web services, in the server, I have a service called display_all_products the code is like so:



case 'display_all_products':
$KEY = filter_input(INPUT_GET, 'api_key');
try {
$token = JWT::decode($KEY, $secret);
$jsonProducts = getProducts();
$products = json_decode($jsonProducts, TRUE);
} catch (Exception $ex) {
return -1;
}
if($products != NULL){
echo $jsonProducts;
}else{
echo "error";
}


Function getProducts() will get all the products information from database and return a json string. When I tested the function, I got a json string like this:



[{"product_id":1,"0":1,"product_name":"Chocolate","1":"Chocolate","price":3,"2":3,"description":"Dark chocolate","3":"Dark chocolate"},{"product_id":2,"0":2,"product_name":"Toilet tissue","1":"Toilet tissue","price":10,"2":10,"description":"Nature and Soft","3":"Nature and Soft"},{"product_id":3,"0":3,"product_name":"Rice","1":"Rice","price":5,"2":5,"description":"Basmati Rice","3":"Basmati Rice"},{"product_id":4,"0":4,"product_name":"Fruit Loaf","1":"Fruit Loaf","price":5,"2":5,"description":"Rich Fruit Bread","3":"Rich Fruit Bread"},{"product_id":5,"0":5,"product_name":"Raspberry Jam","1":"Raspberry Jam","price":6,"2":6,"description":"Full of Flavour","3":"Full of Flavour"}]


It looks like a json string, nothing wrong with it. And also I can get it from the client which means I can do the json_decode in the code I provided above.



The problem is I can not do the json_decode in the client. When it comes to the client, I am getting a json string from the server like this:



[{"product_id":1,"0":1,"product_name":"Chocolate","1":"Chocolate","price":3,"2":3,"description":"Dark chocolate","3":"Dark chocolate"},{"product_id":2,"0":2,"product_name":"Toilet tissue","1":"Toilet tissue","price":10,"2":10,"description":"Nature and Soft","3":"Nature and Soft"},{"product_id":3,"0":3,"product_name":"Rice","1":"Rice","price":5,"2":5,"description":"Basmati Rice","3":"Basmati Rice"},{"product_id":4,"0":4,"product_name":"Fruit Loaf","1":"Fruit Loaf","price":5,"2":5,"description":"Rich Fruit Bread","3":"Rich Fruit Bread"},{"product_id":5,"0":5,"product_name":"Raspberry Jam","1":"Raspberry Jam","price":6,"2":6,"description":"Full of Flavour","3":"Full of Flavour"}]


Both of the json string are very similar but at the very end of the second json string has two more in there. I did not encode twice. I think that is the reason why I can not decode it but where are the two come from and how to get rid of them.



Here is my code form the client:



$keyReq = "?api_key=" . $api_key . "&service=display_all_products";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $basicUrl . $keyReq);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Accept: application/json"));
$jsonProducts = curl_exec($ch);

curl_close($ch);
$products = json_decode($jsonProducts, TRUE);









share|improve this question























  • Got any break statements in that switch? If I had to guess, I'd say some other service calls are executing due to case fallthrough

    – Phil
    Nov 21 '18 at 23:42













  • Yes,there are five services in the server so I have five break statements.

    – BoBOBO
    Nov 22 '18 at 0:32











  • OK,I see what you mean.

    – BoBOBO
    Nov 22 '18 at 0:34











  • Was that the problem then?

    – Phil
    Nov 22 '18 at 1:25











  • There are two more services below that, the two might indicate two arrays from the them. The problem might be case fallthrough but I only want to call the display_all_products service and return only one json string.

    – BoBOBO
    Nov 22 '18 at 9:22














1












1








1








I am building a REST based web services, in the server, I have a service called display_all_products the code is like so:



case 'display_all_products':
$KEY = filter_input(INPUT_GET, 'api_key');
try {
$token = JWT::decode($KEY, $secret);
$jsonProducts = getProducts();
$products = json_decode($jsonProducts, TRUE);
} catch (Exception $ex) {
return -1;
}
if($products != NULL){
echo $jsonProducts;
}else{
echo "error";
}


Function getProducts() will get all the products information from database and return a json string. When I tested the function, I got a json string like this:



[{"product_id":1,"0":1,"product_name":"Chocolate","1":"Chocolate","price":3,"2":3,"description":"Dark chocolate","3":"Dark chocolate"},{"product_id":2,"0":2,"product_name":"Toilet tissue","1":"Toilet tissue","price":10,"2":10,"description":"Nature and Soft","3":"Nature and Soft"},{"product_id":3,"0":3,"product_name":"Rice","1":"Rice","price":5,"2":5,"description":"Basmati Rice","3":"Basmati Rice"},{"product_id":4,"0":4,"product_name":"Fruit Loaf","1":"Fruit Loaf","price":5,"2":5,"description":"Rich Fruit Bread","3":"Rich Fruit Bread"},{"product_id":5,"0":5,"product_name":"Raspberry Jam","1":"Raspberry Jam","price":6,"2":6,"description":"Full of Flavour","3":"Full of Flavour"}]


It looks like a json string, nothing wrong with it. And also I can get it from the client which means I can do the json_decode in the code I provided above.



The problem is I can not do the json_decode in the client. When it comes to the client, I am getting a json string from the server like this:



[{"product_id":1,"0":1,"product_name":"Chocolate","1":"Chocolate","price":3,"2":3,"description":"Dark chocolate","3":"Dark chocolate"},{"product_id":2,"0":2,"product_name":"Toilet tissue","1":"Toilet tissue","price":10,"2":10,"description":"Nature and Soft","3":"Nature and Soft"},{"product_id":3,"0":3,"product_name":"Rice","1":"Rice","price":5,"2":5,"description":"Basmati Rice","3":"Basmati Rice"},{"product_id":4,"0":4,"product_name":"Fruit Loaf","1":"Fruit Loaf","price":5,"2":5,"description":"Rich Fruit Bread","3":"Rich Fruit Bread"},{"product_id":5,"0":5,"product_name":"Raspberry Jam","1":"Raspberry Jam","price":6,"2":6,"description":"Full of Flavour","3":"Full of Flavour"}]


Both of the json string are very similar but at the very end of the second json string has two more in there. I did not encode twice. I think that is the reason why I can not decode it but where are the two come from and how to get rid of them.



Here is my code form the client:



$keyReq = "?api_key=" . $api_key . "&service=display_all_products";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $basicUrl . $keyReq);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Accept: application/json"));
$jsonProducts = curl_exec($ch);

curl_close($ch);
$products = json_decode($jsonProducts, TRUE);









share|improve this question














I am building a REST based web services, in the server, I have a service called display_all_products the code is like so:



case 'display_all_products':
$KEY = filter_input(INPUT_GET, 'api_key');
try {
$token = JWT::decode($KEY, $secret);
$jsonProducts = getProducts();
$products = json_decode($jsonProducts, TRUE);
} catch (Exception $ex) {
return -1;
}
if($products != NULL){
echo $jsonProducts;
}else{
echo "error";
}


Function getProducts() will get all the products information from database and return a json string. When I tested the function, I got a json string like this:



[{"product_id":1,"0":1,"product_name":"Chocolate","1":"Chocolate","price":3,"2":3,"description":"Dark chocolate","3":"Dark chocolate"},{"product_id":2,"0":2,"product_name":"Toilet tissue","1":"Toilet tissue","price":10,"2":10,"description":"Nature and Soft","3":"Nature and Soft"},{"product_id":3,"0":3,"product_name":"Rice","1":"Rice","price":5,"2":5,"description":"Basmati Rice","3":"Basmati Rice"},{"product_id":4,"0":4,"product_name":"Fruit Loaf","1":"Fruit Loaf","price":5,"2":5,"description":"Rich Fruit Bread","3":"Rich Fruit Bread"},{"product_id":5,"0":5,"product_name":"Raspberry Jam","1":"Raspberry Jam","price":6,"2":6,"description":"Full of Flavour","3":"Full of Flavour"}]


It looks like a json string, nothing wrong with it. And also I can get it from the client which means I can do the json_decode in the code I provided above.



The problem is I can not do the json_decode in the client. When it comes to the client, I am getting a json string from the server like this:



[{"product_id":1,"0":1,"product_name":"Chocolate","1":"Chocolate","price":3,"2":3,"description":"Dark chocolate","3":"Dark chocolate"},{"product_id":2,"0":2,"product_name":"Toilet tissue","1":"Toilet tissue","price":10,"2":10,"description":"Nature and Soft","3":"Nature and Soft"},{"product_id":3,"0":3,"product_name":"Rice","1":"Rice","price":5,"2":5,"description":"Basmati Rice","3":"Basmati Rice"},{"product_id":4,"0":4,"product_name":"Fruit Loaf","1":"Fruit Loaf","price":5,"2":5,"description":"Rich Fruit Bread","3":"Rich Fruit Bread"},{"product_id":5,"0":5,"product_name":"Raspberry Jam","1":"Raspberry Jam","price":6,"2":6,"description":"Full of Flavour","3":"Full of Flavour"}]


Both of the json string are very similar but at the very end of the second json string has two more in there. I did not encode twice. I think that is the reason why I can not decode it but where are the two come from and how to get rid of them.



Here is my code form the client:



$keyReq = "?api_key=" . $api_key . "&service=display_all_products";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $basicUrl . $keyReq);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Accept: application/json"));
$jsonProducts = curl_exec($ch);

curl_close($ch);
$products = json_decode($jsonProducts, TRUE);






php rest






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 '18 at 23:11









BoBOBOBoBOBO

61




61













  • Got any break statements in that switch? If I had to guess, I'd say some other service calls are executing due to case fallthrough

    – Phil
    Nov 21 '18 at 23:42













  • Yes,there are five services in the server so I have five break statements.

    – BoBOBO
    Nov 22 '18 at 0:32











  • OK,I see what you mean.

    – BoBOBO
    Nov 22 '18 at 0:34











  • Was that the problem then?

    – Phil
    Nov 22 '18 at 1:25











  • There are two more services below that, the two might indicate two arrays from the them. The problem might be case fallthrough but I only want to call the display_all_products service and return only one json string.

    – BoBOBO
    Nov 22 '18 at 9:22



















  • Got any break statements in that switch? If I had to guess, I'd say some other service calls are executing due to case fallthrough

    – Phil
    Nov 21 '18 at 23:42













  • Yes,there are five services in the server so I have five break statements.

    – BoBOBO
    Nov 22 '18 at 0:32











  • OK,I see what you mean.

    – BoBOBO
    Nov 22 '18 at 0:34











  • Was that the problem then?

    – Phil
    Nov 22 '18 at 1:25











  • There are two more services below that, the two might indicate two arrays from the them. The problem might be case fallthrough but I only want to call the display_all_products service and return only one json string.

    – BoBOBO
    Nov 22 '18 at 9:22

















Got any break statements in that switch? If I had to guess, I'd say some other service calls are executing due to case fallthrough

– Phil
Nov 21 '18 at 23:42







Got any break statements in that switch? If I had to guess, I'd say some other service calls are executing due to case fallthrough

– Phil
Nov 21 '18 at 23:42















Yes,there are five services in the server so I have five break statements.

– BoBOBO
Nov 22 '18 at 0:32





Yes,there are five services in the server so I have five break statements.

– BoBOBO
Nov 22 '18 at 0:32













OK,I see what you mean.

– BoBOBO
Nov 22 '18 at 0:34





OK,I see what you mean.

– BoBOBO
Nov 22 '18 at 0:34













Was that the problem then?

– Phil
Nov 22 '18 at 1:25





Was that the problem then?

– Phil
Nov 22 '18 at 1:25













There are two more services below that, the two might indicate two arrays from the them. The problem might be case fallthrough but I only want to call the display_all_products service and return only one json string.

– BoBOBO
Nov 22 '18 at 9:22





There are two more services below that, the two might indicate two arrays from the them. The problem might be case fallthrough but I only want to call the display_all_products service and return only one json string.

– BoBOBO
Nov 22 '18 at 9:22












0






active

oldest

votes












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53421738%2frest-based-web-services-the-server-returns-more-arrays-than-expected%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53421738%2frest-based-web-services-the-server-returns-more-arrays-than-expected%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Guess what letter conforming each word

Port of Spain

Run scheduled task as local user group (not BUILTIN)