ffpgmbh

RageMP Client Side load JSON file

Recommended Posts

Posted (edited)

Hello RageMP Community,

I am having a hard time figuring out how to load a JSON file into the Client side to generate a menu.

When I do this:

var vehicles = require("./[...]/vehicleshops.json");

and restart and join the Server the Error message says that the file 

"./[...]/vehicleshops.json.js"

could not be found.

I have also tried 

import vehicles from ("./[...]/vehicleshops.json")

With this attempt it has a problem with the overall Syntax, so it doesn't work either.

 

What is the  correct way of loading a JSON file client side, or is there even a working way?

 

Kind regards,

ffpgmbh / Fionn

Edited by ffpgmbh
Corrected mistake from typing file names from head

Share this post


Link to post
Share on other sites

The JSON file is in the subfolder represented by [...] of the client_packages folder.

Share this post


Link to post
Share on other sites

You have to either change your json into exported object in a js file or use
 

require('package://[...]/vehicles.json');

More Info here

Share this post


Link to post
Share on other sites
Posted (edited)

I really appreciate your attempt to help, but when connecting to the server the client says that the file 

"D:\RageMP\client_packages\[...]\package://[...]/vehicleshops.json.js"

could not be found.

What really makes me wonder, is the fact that it looks for a .js file although iI am requesting a .json file.

My code is now

var vehicleConfig = require("package://[...]/vehicleshops.json");

the file vehicleshops.json is present in the downloaded folder, I just double-checked.

Edited by ffpgmbh

Share this post


Link to post
Share on other sites
// shops.js
exports.vehicle = [{"name":"Whatever"}, // etc.];
exports.market = [{"name":"Whatever2"}, // etc.];

// Other clientside file:
const Shops = require('path/to/shops.js');

Shops.vehicle.forEach((shop, idx, arr) => {
	// shop.name;
});

 

Share this post


Link to post
Share on other sites

Thanks to all of you.

 

My final Solution is to just paste my whole json file into a json to js literal converter and paste it into the exports part of a js file, just like MrCarter suggested.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.