Simple JSON unexpected token { at position > 1

json unexpected token colon
json validator
json.parse unexpected token
ruby json parse error unexpected token
unexpected token n in json at position
unexpected token # in json at position 0 nodejs
unexpected token in json at position 193
unexpected token < in json at position 0 angular 6

I'm trying to use JSON simple as part of a Java application to pull specific data from a .json file. Whenever I run the program, I get an unexpected token error message when it tries to parse.

A simplified version of the JSON file is as follows:

{"id":123,"text":"sample1","user":{"id":111,"name":"username"},"lang":"en"}
{"id":345,"text":"sample2","user":{"id":555,"name":"user2"},"lang":"en"}

My code is as follows:

public static void readJSON() {

    JSONParser jsonParser = new JSONParser();
    try {

        FileReader reader = new FileReader(fileLocation);
        JSONObject jsonObject = (JSONObject) jsonParser.parse(reader);
        JSONArray jsonArray = (JSONArray) jsonObject.get("");

        Iterator<?> i = jsonArray.iterator();

        while (i.hasNext()) {
            JSONObject obj = (JSONObject) i.next();
            int tweetID = (int) obj.get("id");
            String lang = (String) obj.get("lang");         
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
} 

In this example, the line of code:

JSONObject jsonObject = (JSONObject) jsonParser.parse(reader);

throws an error due to an unexpected token at the first left brace ({) of the second JSON object (i.e, at the brace before "id":345).

How could I go about resolving this issue? And, as a follow up, how would one also pull the information for the username in this example?

Thanks for taking the time to read through this, and for any assistance provided!

That file is an invalid JSON file, it contains an entire object on each line.

What you'll need to do is read the file line by line, and then passing each line to the parser to create a new object.

fixing SyntaxError: Unexpected string/token in JSON at position , fixing SyntaxError: Unexpected string/token in JSON at position Outside of simple configuration files, you should avoid serializing JSON yourself. This response started with a < (hence the “Unexpected token <”). That unexpected token, <, is a strong clue that the response was HTML instead of JSON. The root cause is that the server returned HTML or some other non-JSON string. Why would it do that? “Unexpected token o in JSON at position 1” and other varieties

You are trying to parse JSON object, and you actually have two JSON objects. And in your code you are actually expect an array of JSON objects. So, use the proper JSON array:

[
{"id":123,"text":"sample1","user":{"id":111,"name":"username"},"lang":"en"},
{"id":345,"text":"sample2","user":{"id":555,"name":"user2"},"lang":"en"}
]

In order to quickly check your JSON syntax, you can use some online tool.

How to fix Unexpected Token in JSON error, Getting this error? Unhandled Rejection (SyntaxError): Unexpected token in JSON at position Duration: 5:10 Posted: Aug 21, 2018 SyntaxError: Unexpected token < in JSON at position 0 at JSON.parse (<anonymous>) at Q (angular.js:1333) at Ot (angular.js:10635) at angular.js:10726 at r (angular.js

If you fix your code, you can solve unexpected token error.

public static void readJSON() {

JSONParser jsonParser = new JSONParser();
try {

    FileReader reader = new FileReader(fileLocation);

    //You need to fix this part
    JSONArray jsonArray = (JSONArray) jsonParser.parse(reader);

    Iterator<?> i = jsonArray.iterator();

    while (i.hasNext()) {
        JSONObject obj = (JSONObject) i.next();
        int tweetID = (int) obj.get("id");
        String lang = (String) obj.get("lang");         
    }
} catch (Exception e) {
    e.printStackTrace();
}
} 

Getting "Unexpected token ':'" from a simple JSON string, var info = eval(req.responseText); // req.responseText = {id: 2, title: "KVillains", description: "KVillains are an American punk rock band from Knoxville, TN and� position: 24 Unexpected token RIGHT BRACE(}) at position 24. JSON.simple - Container Factory. ContainerFactory can be used to create Custom container for parsed JSON objects/arrays. First we need to create a ContainerFactory object and then use it in parse Method of JSONParser to get the required object. See the example below − Example

Your JSON is indeed wrong. It actually contains 2 valid JSONs. If you want to create one valid JSON document you have to wrap your input with { and } or [ ] if this is an array or collection. Please note the comma that separates 2 different entities.

[
{"id":123,"text":"sample1","user":{"id":111,"name":"username"},"lang":"en"},
{"id":345,"text":"sample2","user":{"id":555,"name":"user2"},"lang":"en"}
]

How to fix: "unexpected token" error for JSON.parse, When an AJAX request raises an exception on the server, Rails will show a minimal error page with only basic information. Because all Unpoly updates work using� Today i am show you how to remove Unexpected token f in JSON at position 192 - How to Fix It simple Way To Remove This ERROR. Window to Android - Best Software Make lots of Android Mobile In your

SyntaxError: Unexpected token < in JSON at position 0 , I can literally copy the examples from the link to my code and the validation code still throws an error where expectation would be for it to return {"� Valid JSON but JSON Input Error: [Unexpected token END OF FILE at position ]! Results 1 to 2 of 2 [Unexpected token END OF FILE at position ]!

Unexpected token < in JSON at position 0, “Unexpected token o in JSON at position 1” and other varieties. The exact text of this error will differ depending on what the server returned. The token and the position may vary, but the root cause is the same: the text that your app is trying to parse as JSON is not actually valid JSON. I'm getting a json string response from the server and trying to parse with json.parse(string) but always get the same error: Uncaught SyntaxError: Unexpected token ' in JSON at position 1 at JSON.parse () at :1:6. Anyone can help me with this? I can't see the problem. Here is the raw string formated:

Unexpected token json, Uncaught SyntaxError: Unexpected token M in JSON at position 0. 2 : php > var_dump(json_decode('this is a simple string')); NULL I had several functions� I've an encoded stringifyed JSON object stored in database, I decoded it and loaded it and tried to parse it into an object But I get. Uncaught SyntaxError: Unexpected token ' in JSON at position 2 at JSON.parse Code: var attr = new Object(); attr = JSON.parse(code[1].replace(/"/g, "'")); Object decoded:

Comments
  • Essentially this is {}{} which is invalid in JSON. The JSON either needs to be an array in the file itself [{}, {}] or you need to parse it one line at a time
  • Your JSON is invalid I mean in a wrong format.
  • Read my answer. This may be a valid json depending on the data model.