0

I have a json sample menulist.json (attached in pastebin) that needs to be read in jsavascript. I'm reading file as mentioned in this answer.

But looks like JavaScript is not reading JSON properly. Beverages and Desserts should be under Menu. But it doesn't seem to be happening: JSON IN Chrome Console

Just to verify same json, I pasted that in python shell. It looks like python is reading properly. JSON in Python console

Here is my code: HTML

<html>
  <head>
  ...
  ...
  ...
        <script type="text/javascript" src="menulist.json"></script>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
        <script type="text/javascript" src="js/index.js"></script>
    </head>
     <body> 
          ...
          ...
          ...
     </body>
    </html

Note : It's not about the ordering or sequence. It's about the child objects not appearing under same key.

Community
  • 1
  • 1
Laxmikant
  • 2,046
  • 3
  • 30
  • 44
  • Why dont you get it through $.getJSON ? In your JSON, menu is an array containing one object with Beverages right inside. – Loenix Jul 24 '16 at 07:22
  • @Loenix I don't think you're right about that. It's indented weirdly, but `Beverages` is nested under `Menu` in the JavaScript that was shared. – user94559 Jul 24 '16 at 07:23
  • @Laxmikant I agree that in the JavaScript you shared, `Beverages` and `Desserts` are nested inside `Menu`. I would assume, then, that this is not actually the JavaScript you're executing. Things to check: perhaps that's a copy of the file and not the actual file in use, or perhaps the browser is caching an old version of the file that has a different structure. The "Network" tab in Chrome's developer tools should let you check what the actual downloaded JavaScript looks like. – user94559 Jul 24 '16 at 07:25
  • 1
    @t.niese I don't think the ordering was the issue; it's the structure. (When they say "under" they mean in the tree sense, i.e. "nested inside.") – user94559 Jul 24 '16 at 07:26
  • @t.niese - Yes, I'm not talking about the ordering. – Laxmikant Jul 24 '16 at 07:27
  • Sorry my fault remove the vote and comment. – t.niese Jul 24 '16 at 07:29
  • @smarx I used a JSON formatter to read it. Menu contains an array with one object containing Veg, Non-veg, Desserts, Beverages & other. – Loenix Jul 24 '16 at 07:29
  • @smarx : Let me check – Laxmikant Jul 24 '16 at 07:30
  • @smarx : You were right, It was cachign issue. But why it has happened I have never changed the file. It's as it is from when I started the coding. – Laxmikant Jul 24 '16 at 07:32
  • The console.log is right... I described what your json is... – Loenix Jul 24 '16 at 07:32
  • @Loenix Sorry, confused the names. :-) Yes, what you just described is right. Maybe I misunderstood your first comment? – user94559 Jul 24 '16 at 07:32
  • As a note: The file you attached with your pastbin is not JSON (JSON is a 100% textual representation of data), the file containing JavaScript code where you append an object to `data`. – t.niese Jul 24 '16 at 07:33
  • @Loenix : but from the attached answer, it says start a server to read the JSON file with $.getJSON. I don't have to do that. – Laxmikant Jul 24 '16 at 07:34
  • @t.niese : Yes, I did that because it was mentioned in this answer http://stackoverflow.com/a/24378510/4881948 – Laxmikant Jul 24 '16 at 07:35
  • 1
    @Laxmikant "I have never changed the file. It's as it is from when I started the coding." <-- Apparently that's not true :-) – user94559 Jul 24 '16 at 07:36
  • It looks like a JSONP but i think, he should use a function call. i don't recommend JSONP, prefer pure JSON. – Loenix Jul 24 '16 at 07:36
  • @smarx : I aplogize if i might have changed it. But what I remember is I didn't change it. Please add your comment as an answer. – Laxmikant Jul 24 '16 at 07:37
  • @Laxmikant There they assigned a JSON formatted string to `data`, so it might be somehow _ok_ ( I still wouldn't insist not not do so) to call the file `.json`, and there you need to then parse the `data`. In your example the file (except that it might look like JSON) is not JSON. If you want to keep your project maintainable you should avoid those inaccuracies. JSON has to be parsed, before it is used. – t.niese Jul 24 '16 at 07:39
  • 1
    Totally agreed with @t.niese. There's no JSON anywhere in your project. Rename `menulist.json` to `menulist.js` and just know that what you're doing is including a script that defines a global variable called `data` with an object in it. – user94559 Jul 24 '16 at 07:41
  • Thanks for the suggestions, Will try that. – Laxmikant Jul 24 '16 at 07:42

1 Answers1

1

(transferring comment to answer)

The JavaScript in the Pastebin defines an object with the structure you're expecting, so that must not be the JavaScript you're actually executing. Per the comment discussion above, it turned out that the browser was caching an old version of the file (when the object apparently had a different structure).

user94559
  • 59,196
  • 6
  • 103
  • 103