2

I've been trying to store the data from a JSON file into a variable in Javascript and everything were good until the JSON.parse function didn't work.

My file is called test.json and it looks like this:

{
  test: 'Hello World!'
}

I also tried changing it to:

{ test: 'Hello World' }

My javascript code is this:

var a = OwNet.get( 'core/config/test.json', function( Res ) {

 // Res is the response from an AJAX request (where i requested the test.json file)

  if( Res !== "" && Res !== undefined && Res !== null ) {

    // Here i tried to replace the line breaks, carriage returns and spaces. It failed.
    // (I also tried to remove it)
    Res.replace( /\r\n|\r|\n|\s*/gm, "" );

    // Here i tried to transform Res in an object
    tmp = JSON.stringify( Res );
    return JSON.parse( tmp ); // This returns a string instead an object

  } else return null;

});

The only problem is that the variable 'a' isn't an object, instead it is a string, i've been looking for the answer but i haven't could.

6
  • Is JSON.parse( tmp ) returning any error??? Commented Apr 14, 2015 at 6:21
  • 1
    Why are you calling stringify? You're trying to de-serialize, aren't you? Commented Apr 14, 2015 at 6:22
  • No that isn't. There's no errors. I used stringify because i've read i had to add it before JSON.parse Commented Apr 14, 2015 at 6:22
  • Are you trying to replace newline characters within the JSON object, or the actual linebreaks within the object itself? If it's the latter, you don't have to. Calling JSON.stringify will remove all line breaks and convert your JSON into a string. Commented Apr 14, 2015 at 6:25
  • When I used stringify i wanted to convert Res to string and then use it in JSON.parse Commented Apr 14, 2015 at 6:26

4 Answers 4

6

Your JSON is not valid, change to

{
    "test": "HelloWorld!"
}
Sign up to request clarification or add additional context in comments.

Comments

0

Your JSON format is wrong, change to

{ "test": "Hello World!" }

https://msdn.microsoft.com/en-us/library/bb299886.aspx

Comments

0

Thank you so much everyone, the answer was easier than i though:

The file test.json:

{
  "test": "Hello World!"
}

And the code:

var a = OwNet.get( 'core/config/test.json', function( Res ) {

// Res is the response from an AJAX request (where i requested the test.json file)

  if( Res !== "" && Res !== undefined && Res !== null ) {

    // Replacing the line breaks, carriage returns and spaces
    Res = Res.replace( /\r\n|\r|\n|\s*/gm, "" );

    // Erased the JSON.stringify and replaced tmp by Res
    return JSON.parse( Res );

  } else return null;

});

4 Comments

You should approve some answer.
Ok, sorry i have never been in a forum. @AlexanderGorelik and someone whose answer i've seen before. The last one i mencioned gave me the answer to Res = Res.replace ... but i can't find his/her name
One more question, how can i close this topic??
In order to close you just need to approve the best answer as you already did.
0

{ "test": "Hello World!" }, the test is a string and needs to be presented like that .

Comments

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.