2

I have a json like this :

{
"One\-test": {
            "name" : "One\-test",
            "link" : "xxx"              
        },
"Two\-test": {
            "name" : "Two\-test",
            "link" : "yyy"              
        }
}

and in my javascript file I use a jQuery call

var myJson;
jQuery.getJSON('path/myJson.json', function (data) {
     myJson = data;
    });

Unfortunately the call doesn't succeed, in the sense that myJson = data is not executed (I tried to put a console message just before the statement but it is not executed) and the variable myJson is still undefined (I waited for the end of the call with a $.when statement and I printed out myJson). Most likely the issue could be the format of names One\-test and Two\-test (chars '\' and '-' ), because the path is correct (I'm pretty sure). I cannot change those names, then I have to think about something else. Any idea? I tried with an ajax call like this :

function getJson(myJson){
  $.ajax({
        type: "GET",
        url: "path/myJson.json",
        data: {},
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success:
          function(data) {
                 myJson = data;                
                     }
             });
         }      

but result is still the same.

EDIT

If I try to execute the same code with :

  {
    "Onetest": {
                "name" : "One\-test",
                "link" : "xxx"              
            },
    "Twotest": {
                "name" : "Two\-test",
                "link" : "yyy"              
            }
    }

it succeed. This is another proof of "bad format"

11
  • Where did you define myJson var? You can declare it like this inside your function: var myJson = data; Commented Mar 16, 2015 at 12:32
  • @bcesars: Right above the call. Commented Mar 16, 2015 at 12:33
  • Try doing var myJson = JSON.parse(data); Maybe it is returning a string? Commented Mar 16, 2015 at 12:35
  • 1
    what is path? where does it point to? external domain? same domain, different folder? is it http or file protocol that you are trying to access? Commented Mar 16, 2015 at 12:47
  • 1
    @FabrizioMorello: "balexandre what is the difference?" It could make a big difference, but your edit suggests that it doesn't in your particular case. Basically, doing XHR on file:// URLs doesn't work in some browsers. Commented Mar 16, 2015 at 12:56

1 Answer 1

3

If I try to execute the same code with :

...

it succeed. This is another proof of "bad format"

Okay, that tells us that the JSON parser being used is interpreting those "One\-test" strings as invalid. You could read http://json.org that way (and it appears that the http://jsonlint.com folks do, as it also rejects those strings), although the RFC says

Any character may be escaped.

...which argues that while pointless, the \ before the - shouldn't be an error. But perhaps the RFC means any character can be escaped as a Unicode escape or similar.

The best thing would be what you said you can't do: Fix the JSON, removing the at-best-unnecessary-at-worst-problematic \ before -.

The next best thing would be to do some pre-processing. You could do straight string replacement by telling jQuery not to try to parse the JSON:

$.ajax({
    type: "GET",
    url: "path/myJson.json",
    data: {},
    dataType: "text",        // <=== Don't parse it
    success: function(data) {
        // ...
    }
});

then in success doing:

myJson = JSON.parse(data.replace(/\\-/g, "-"));

...though of course that's quite a naive replacement.

Here's a demo of that second bit:

var data =
  '{' +
  '"One\-test": {' +
  '            "name" : "One\-test",' +
  '            "link" : "xxx"          ' +    
  '        },' +
  '"Two\-test": {' +
  '            "name" : "Two\-test",' +
  '            "link" : "yyy"          ' +    
  '        }' +
  '}';
var myJson = JSON.parse(data.replace(/\\-/g, "-"));
snippet.log(myJson["One-test"].name); // "One-test"
<!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>

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

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.