7

I have a couple JSON files that are structured like this (let's call this info.json):

{
  'data': {
    'title': 'Job',
    'company': 'Company',
    'past': [
      'fulltime': [
        'Former Company'
      ],
      'intern': [
        'Women & IT',
        'Priority 5'
      ]
    ],
    'hobbies': [
      'playing guitar',
      'singing karaoke',
      'playing Minecraft',
    ]
  }
}

And in a separate JavaScript file, I have a function that looks like this:

function getJSONInfo() {
  fetch('info.json').then(function(response) {
    return response.json();
  }).then(function(j) {
    console.log(j);
  });
}

And I keep getting this error when I run getJSONInfo():

Uncaught (in promise) SyntaxError: Unexpected token '

What am I missing? I don't have a stray ' anywhere so I'm not sure what's wrong.

2
  • I'm getting the same issue too, but my response is coming from .NET OAuth code, which was working for me up until recently. Can you remember what the issue was? Commented Dec 17, 2016 at 17:09
  • @Tom it was because I used single quotes instead of double quotes, it's invalid JSON to use single ones! Commented Dec 18, 2016 at 6:57

2 Answers 2

7

You need to have double quotes for your attributes for valid json.

You can use json validators such as http://jsonlint.com/ to check if your syntax is correct.

Also, as shayanypn pointed out, "past" should be an object, rather than an array. You are trying to define "past" as an object literal but are using square brackets to denote an array.

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

Comments

2

you is invalid at all

1- you should use double quotes

2- bad syntax of object attribute

"past": [
    "fulltime": [
        "Former Company"
    ],
    "intern": [
        "Women & IT",
        "Priority 5"
    ]
],

it should bed

"past": {
    "fulltime": [
        "Former Company"
    ],
    "intern": [
        "Women & IT",
        "Priority 5"
    ]
},

your valid json is

{
    "data": {
        "title": "Job",
        "company": "Company",
        "past": {
            "fulltime": [
                "Former Company"
            ],
            "intern": [
                "Women & IT",
                "Priority 5"
            ]
        },
        "hobbies": [
            "playing guitar",
            "singing karaoke",
            "playing Minecraft"
        ]
    }
}

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.