2

I am trying to create a simple laravel crud, where I will be able to use a json file as database instead of MySQL. But it's returning "null" when I am trying to fetch data from the json file located in "resource/lang" folder .

The output is :

Please click to view the Image

My codes from controller :

    public function getjson(){

    $json = file_get_contents('../resources/lang/test.json');
    $gravatar = json_decode($json);
    dd($gravatar);
   }

My json file (locating at resources/lang/test.json)

[
{   
    "id": 1,
    "product_name": "Apple",
    "per_item_price": "10",
    "product_quanity": "4",
    "total_price": "50",
},

{
  
    "id": 2,
    "product_name": "Apple",
    "per_item_price": "10",
    "product_quanity": "4",
    "total_price": "50",
},
]

And The route from web.php is :

Route::get('/getjson', 'App\Http\Controllers\Master@getjson');

test.json file screenshot

dd($json result screenshot)

2
  • 1
    what happens if you dd($json); or dd(json_last_error())? Commented Jun 19, 2021 at 11:03
  • for dd(json_last_error()), It returns 0, and for dd($json), I am getting ` """ { "product_name": "Apple", "per_item_price": "per_item_price", "product_quanity": "product_quanity", "total_price": "total_price", } """ ` Commented Jun 19, 2021 at 11:16

1 Answer 1

2

The issue is with JSON in test.json. Because it has a trailing comma.

[
    {
        "id": 1,
        "product_name": "Apple",
        "per_item_price": "10",
        "product_quanity": "4",
        "total_price": "50"
    },

    {

        "id": 2,
        "product_name": "Apple",
        "per_item_price": "10",
        "product_quanity": "4",
        "total_price": "50"
    }
]

Remove unwanted comma after total price and end of last object. Language folder used for multi-language purpose. So I suggest you to use storage folder to save this file

For accessing storage files storage/app/test.json

$json = \Illuminate\Support\Facades\Storage::get("test.json");
    $gravatar = json_decode($json);
    dd($gravatar);

Updated As above comment by @shaedrich.

To check JSON error

$json = \Illuminate\Support\Facades\Storage::get("test.json");
    $gravatar = json_decode($json);
    switch (json_last_error()) {
        case JSON_ERROR_NONE:
            echo ' - No errors';
            break;
        case JSON_ERROR_DEPTH:
            echo ' - Maximum stack depth exceeded';
            break;
        case JSON_ERROR_STATE_MISMATCH:
            echo ' - Underflow or the modes mismatch';
            break;
        case JSON_ERROR_CTRL_CHAR:
            echo ' - Unexpected control character found';
            break;
        case JSON_ERROR_SYNTAX:
            echo ' - Syntax error, malformed JSON';
            break;
        case JSON_ERROR_UTF8:
            echo ' - Malformed UTF-8 characters, possibly incorrectly encoded';
            break;
        default:
            echo ' - Unknown error';
            break;
    }
Sign up to request clarification or add additional context in comments.

21 Comments

I copy pasted your code, still giving the same result ...
i have tested it .it works fine.check again once
adding file to storage doesn't fix error from json file. I suggested to keep files in necessary folder
Thank you so much bro ! I mistakenly copied my same defected json file in storage instead the corrected one - sorry for that
@azim np :) .Even i curious why it didn't worked for you.any way glad to here you have solved your issue
|

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.