-1

I am developing an API using core PHP. When I am returning data, all the fields data are returning as a string, but in some fields I have JSON type of data which is also coming as string

Example:

end_city: "mysore",
facilities: "{"Breakfast":false,"PickDrop":true,"Hotel":false,"Guide":true}"

Here, field facilities is having JSON, but getting this as a string in the response, want to get this as JSON only not string.

<?php
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: POST, GET, DELETE, PUT');
header('Access-Control-Allow-Headers: token, Content-Type');

// get database connection
require_once '../config/database.php';

// instantiate tours object
include_once '../objects/tours.php';

$db = new Database();
$tours = new Tours($db);

$tour = $tours->getTour($_REQUEST["t"]);
$num = $tour->num_rows;

if ($num > 0) {
    while ($row = $tour->fetch_assoc()) {
        echo json_encode($row);
    }
    // set response code - 200 OK
    http_response_code(200);
} else {
    // set response code - 404 Not found
    http_response_code(404);
    // tell the user no products found
    echo json_encode(
        array("message" => "No tour found.")
    );
}

The exact scenario is, I have one form , after submit that form , I get the JSON data in some fields , after getting the all data I save that in mysql using core php , now I have to display that data on page using angualrjs . Now when I do get request , I get data in the JSON format and all the fields are in the type of string , even JSON fields I am getting as string which create problem.

3
  • i think http_response_code() suppose to be before any echo() Commented Jul 6, 2019 at 22:38
  • No that create no issue Commented Jul 7, 2019 at 11:22
  • This question is better to be closed as off topic and deleted but realistically we can only close it as a dupe Commented Dec 20, 2024 at 9:24

4 Answers 4

1

Use json_decode to convert a string to json

https://www.php.net/manual/en/function.json-decode.php

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

12 Comments

data in not in string format but in mysql query i am getting data as string. I already try json_decode but getting null as data is in right format
what type of column in the table where is the json stored?
I am storing data in mysql , and using data type text
if you save in the text type, then you return the string
Can you suggest me the valid data type for JSON in mysql
|
1
  1. Set the status code and send headers before sending the content.
  2. Do not echo multiple json values in the loop, it wouldn't be a valid json response. Collect all results into a list, json_encode it then send that.
  3. Set content type to application/json

Change:


if ($num > 0) {
    while ($row = $tour->fetch_assoc()) {
        echo json_encode($row);
    }
    // set response code - 200 OK
    http_response_code(200);

into

header('content-type: application/json');
if ($num > 0) {
    http_response_code(200);
    $results = []
    while ($row = $tour->fetch_assoc()) {
        $results[] = $row;
    }
    echo json_encode($results);

Comments

-1
json_decode(str_replace("'",'"',$sqldatacell),true)

1 Comment

This is exemplary bad suggestion
-2

The above issue is solved and the issue is my data is not saving correctly in the database that's why JSON.parse is not working properly but now issue is solved using JSON.parse

1 Comment

Don't just say it's solved, show how you solved it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.