Loading a JSON file in a cocos creator project?

Note that I’m very new to this. I have a simple JSON file under assets and I want to load it in a variable like I do in other langages.

How can I do that?

Something like that:

var HeroDefault = ccs.load(“HeroDefault.json”);

I get the error that
Simulator : HeroDefault.json does not exist

When I use the simulator.

What is the best way to load a JSON file in the creator? What Am I doing wrong?


Hi, try this…

  1. right place:
    put u’r json file to path “assets/resources/HeroDefault.json”
    if assets not has resources sub folder, create it.
  2. Coding:
var url = cc.url.raw( 'resources/HeroDefault.json' )
cc.loader.load( url, function( err, res)
    cc.log( 'load['+ url +'], err['+err+'] result: ' + JSON.stringify( res ) );

then you will see the json file dump to log.


It worked, thanks alot.

how we can use the json in cocos creator 2.1.0

var t_Setsjson;

var url = cc.url.raw( ‘resources/IV_SetsJSON.json’ )

cc.loader.load( url, function( err, t_Setsjson)


cc.log( ‘load[’+ url +’], err[’+err+’] result: ’ + JSON.stringify( t_Setsjson ) );

starting its working after buid in browser getting error can u check please

after running in browser i am getting error called

Failed to load resource: the server responded with a status of 404 (Not Found) [http://localhost:7456/resources/IV_SetsJSON.json]

This is a 3 year old topic, why not start a new topic?

I just need the one line of code for reading a string containing a json and making a json object.
for e.g…
var txt = {“name”:“sam”};
var objj0 = JSON.stringify(txt);
var txt2 = objj0[“name”];
it prints undefined in the console.
what is wrong and how can I assign the value of txt2 to another variable of type string?

because you are reading it as an object after you converted it to string

I guess what you are trying to do is following:

var txt = '{"name":"sam"}';   //String in JSON format, please notice the single quotes surrounding the string
var obj0 = JSON.parse(txt); //Take text and create object
var txt2 = obj0["name"];
console.log(txt2); //this *should* print out "sam"

JSON.stringify(obj) takes an object and converts it to a string, but considering your purpose description, I guess you want to take a string and convert it to an object, which can be achieved by JSON.parse(string)

Create new a js, and call new variable equal that json, then export module and require it in other js
Create new js file named “Data.js”
var new_json = […] //content your json you need loaded
var new_json2 = […] //content your json you need loaded

module.exports = { //export data
new_json: new_json,
new_json1: new_json1,


Then, in other js, you need require this file

var {new_json, new_json1, …} = require(’./Data’)

Hope resolve for your problem :slight_smile:

NewProject_1 (1).zip (287.3 KB)
i have typescript code with loading json, can you give me javascript code for loading json i got errors …

Hi, you can try this:
put your json file to path “assets/resources/config.json”
cc.loader.loadRes(‘config’, cc.JsonAsset, function( err, res) {
cc.log("data: ", res.json);

I tried with Cocos Creator v2.1.3 and it working
Hope resolve for your problem :slightly_smiling_face: :slightly_smiling_face:

No sir, iam getting config as error

i have json data , i want load in content scrollview.i want json loading in content code,not for json loading in cc,log cosole.i want to see json data in scrollview in content. Any one please help me

this is my json text data, in this 13 lines of text, i want to load in scrollview in content,any one give code in javascript.


with properties cc.class also give ??

I think you should change your json file like this

    "SymptomsData": [

And this is my code load data from json file in scrollview in content

cc.loader.loadRes('jsondummy', cc.JsonAsset, function( err, res) {

    var dataJson = res.json.SymptomsData;

    for(let element in dataJson) {




Sir, this is small task but i cann’t do it??? where is the mistake done i don’t know, i tried your code also but don’t get it,i am using cocos creator v 2.2.1 version.

  1. can you please make me a design and code ,take any json text data ,that will be displayed on content in scrolleview.
  2. waiting for you responce

you change line 43 code like this: