Page 1 of 2

How to debug JSON errors

Posted: 30 Jun 2018, 11:18
by Bearbear76
Contents
  1. Introduction
    Plugin Creator Website (PCA)
  2. Error Message
  3. Common Words in JSON
  4. List of Errors
    Syntax Errors (1 listed)
    TheoTown Errors (0 listed)
Introduction

Any plug-in creator will bump into JSON errors. It is usually not that hard to debug once you get used to it.
JSON errors are pretty calm & easy to fix compared to Lua (A scripting language used for plug-ins),
for such errors I would also suggest using third-party validators to debug your JSON such as: Jsonlint.
However, sometimes you can get a nasty error which could seem impossible to fix but do not worry this thread will teach
the basics of debugging a JSON file
and also list some solutions to known errors.


Plugin Creator Website (PCW)

Plugin Creator Website or PCW is a community developed tool by JustAnyone.
PCA is a well developed tool that allows people who are not familiar with JSON to easily create JSON while avoiding syntax errors.
If you are new to JSON, I would recommend using it. People who would like to make more complicated JSON using functions should refrain from using it as functions will not be supported in the near future.

Error Message

Whenever you run a JSON containing an error within TheoTown displays an error message at the beginning; during initialisation.
This error message contains useful information such as the directory or location in where the error occurred and the type of error that occurred.
Now, let's look at an error message that contains, quite frankly, the most common error in JSON.


In /.../TheoTown/plugins/MyPlugin/plugin.json:

util.json.JSONException: Unterminated object at character x.


The first line indicates where the error occurred. In this case it would be in a JSON called 'plugin.json' inside a folder called 'MyPlugin'.
Note: /.../ is where the directory of the TheoTown folder. This varies between devices such as Android or Windows etc...


In /.../TheoTown/plugins/MyPlugin/plugin.json:

The second like specifies which error you got at what character. Although I would prefer it to show at which line the error occurred rather than character for convenience's sake, but we have to work with what is given.
With characters you can somewhat guess where the error occurred especially in long JSON code, but that is a skill that you can only develop with practice.


util.json.JSONException: Unterminated object at character x.

Now that you have learnt how to read error messages given by TheoTown you can start editing JSON files to fix errors!

Common Words in JSON

These are some words you may want to know when working with JSON.

keys: Keys are basically tags, such as "id" or "type".

[{
"id":"$park01",
"type":"park"
}]


values: Values are the information inside keys. keys and values are always in pairs in JSON.

[{
"id":"$park01",
"type":"park"
}]


Objects: There are two types of objects in JSON, main objects (highlighted in yellow), and key-value pair objects (highlighted in blue).

[{
"id":"$park01",
"type":"park"

}]



Arrays: Arrays are boxes that can contain objects and also values.

[{
"id":"$park01",
"type:"park"
}]


List of Errors

This is where most of the known errors which occurred in the past will be documented.

Syntax Errors

Syntax errors are simply grammatical errors. Think of it as if you wrote 'I have many dog' in English, JSON follows are structure of rules that must be followed and if you make grammatical mistakes it will throw out an error. However, JSON syntax isn't that convoluted so it is relatively easy to write.

util.json.JSONException: Unterminated object at character x.

Solution: Object wasn't terminated properly. Usually missing a comma or curly bracket at the end of an object at character x.

util.json.JSONException: Expected ':' after type at character x.

Solution: Missing a : after key at character x.

util.json.JSONException: Expected literal value at character x.

Solution: The opposite of unterminated object. Has an excess comma at character x.
General tip would be to not put a comma behind a } such as:


[{
...
"frames":[{"bmp":"example.png",}],
}
]

TheoTown Errors

TheoTown errors are not syntax errors, but errors caused by TheoTown specific JSON rules in order to load a plugin properly. Therefore these errors are not detectable by 3rd party validators such as Jsonlint and only by TheoTown.

java.lang.illegal.ArgumentException: May not find a loader for type not defined.

Solution: Probably a typo of the type key, or type was not defined (missing type).

java.lang.illegal.ArgumentException: May not find a loader for type (value).

Solution: Probably a typo of the type value, or the entered value doesn't exist.

java.lang.illegalStateException: When loading "null": No value for id.

Solution: Probably a typo of the id key, or id was not defined (missing id).

java.lang.illegalStateException: Please specify width and height attributes. They have to be equal and >= 1.

Solution: General problem with width or height object. Width and height must be equal and greater or equal to 1. Could also be a typo.
The following will give you an error:


"with":1, <-- typo of width
"height":0, <-- less than 1 + not equal values

---WIP---

Re: Common plugin errors

Posted: 30 Jun 2018, 17:38
by The26
Very useful for beginner plugin creators. Great job! :)

Re: Common plugin errors

Posted: 30 Jun 2018, 17:39
by CommanderABab
Bearbear65 wrote:
30 Jun 2018, 11:18
Hello!
Here I will list some common plugin errors.
I wrote the solution and the definition of the error message you get when you open Theotown.
This was based on the majority of the plugin errors sent by people

You can help expand this list by writing an error with the solution! :)

Code: Select all

org.json.JSONException: Unterminated object at character x.
Definition: This means that the plugin wasn't terminated in a specific line. ( doesn't have a comma at the end of a line)
Solution: Add a comma to the line which doesn't include a comma.

Code: Select all

org.json.JSONException: Expected literal value at character x.
Definition: Simply, the opposite of "Unterminated object". Means that you have put a comma in where you didn't have to.
Solution: Remove the extra comma that you added. ( Remember that commas don't precede }] )

Code: Select all

java.lang.IllegalArgumentException: Id x is already in use. Use another id or add "override":true to your plugin.
Definition: Means that the given Id was already in use, in other words a plugin with the same id exists in your plugin folder or Theotown.
Solution: There are many causes for this, e.g. Downloaded a plugin twice, put both the folder and zip inside the plugin folder etc...
So, the easiest way to solve this is by adding "override":true, or changing the id.
If you're trying to replace a in-game building then you must add "override":true, for it to be overridden.

Code: Select all

java.lang.IllegalArgumentException: Cannot fit bmp x.png of size y into world texture (maybe too many plugins?)
Definition: As it says, it means that you have unfortunately reached the limit of the world texture. (space for plugin texture)
Solution: Clean your texture space by deleting unnecessary plugins, or give up on the plugin you wanted to add.

Code: Select all

java.lang.IllegalArgumentException: No category x could be found
Definition: It means that it could not have found the category that is required for the plugin.
Solution: Check if you have downloaded the category. Some plugin creators include the download for the category on the download page, some are
Already in the plugin. In this case it's usually because you have not downloaded the separate category for the plugin. Check the download page once more to see if you can find it.


The best thing to do is not to edit the file unnecessarily! :json
The26 wrote:
30 Jun 2018, 17:38
Very useful for beginner plugin creators. Great job! :)
Quotes for posperity sake. :)

Re: Common plugin errors

Posted: 22 Nov 2018, 01:42
by Between3Characters
What about the jsonobject cannot be converted to jsonarray? I got that problem, but then i check syntax and it says valid.... Or did i miss something? For example, image? Its a category though..

Re: Common plugin errors

Posted: 22 Nov 2018, 01:51
by Lobby
You probably used {} where [] is needed. E.g. you might have forgot to put [] around a frame:

Code: Select all

"frames":[{...}]
instead of

Code: Select all

"frames":{...}

Re: Common plugin errors

Posted: 02 Dec 2018, 00:29
by Mrqwerty
Thx :json

Re: Common plugin errors

Posted: 05 Dec 2018, 03:03
by Ahmad Nur Aizat
How about the height limit error?

I can't sometimes balance the height required

Re: Common plugin errors

Posted: 16 Jan 2019, 13:15
by Between3Characters
Lobby wrote:
22 Nov 2018, 01:51
You probably used {} where [] is needed. E.g. you might have forgot to put [] around a frame:

Code: Select all

"frames":[{...}]
instead of

Code: Select all

"frames":{...}
I am on the way to make a new park category. I still got an error, even if i already put that on frames. Whats going on? Check the last post in this forum.

Re: How to debug JSON errors

Posted: 20 Nov 2019, 14:13
by Bearbear76
Currently working on a new version. Sorry if you are trying to use this post for guidance.

Re: How to debug JSON errors

Posted: 23 Nov 2019, 17:20
by JustAnyone
This will be reference page for PCA.

Re: How to debug JSON errors

Posted: 24 Nov 2019, 08:39
by Bearbear76
JustAnyone wrote:
23 Nov 2019, 17:20
This will be reference page for PCA.
added PCA document

Re: How to debug JSON errors

Posted: 24 Nov 2019, 11:44
by JustAnyone
well developed - good joke, regardless what do you mean by "functions will not be supported in the near future"?

Re: How to debug JSON errors

Posted: 24 Nov 2019, 11:55
by Bearbear76
JustAnyone wrote:
24 Nov 2019, 11:44
well developed - good joke, regardless what do you mean by "functions will not be supported in the near future"?
you said PCW won't support fun but just in case I made it 'not in the near future'. maybe my wording was a bit off

Re: How to debug JSON errors

Posted: 10 Feb 2021, 11:44
by Bearbear76
I got to clean this post up :lol:

Re: How to debug JSON errors

Posted: 10 May 2021, 05:58
by 1Code

Re: How to debug JSON errors

Posted: 15 May 2021, 11:28
by Bearbear76
Nice video looks like a weta (the bug). :)
Bearbear76 wrote:
10 Feb 2021, 11:44
I got to clean this post up :lol:
This post is pretty messy and I want to fix it but I don't have much time... :(

Re: How to debug JSON errors

Posted: 08 Feb 2022, 04:58
by 1Code
20220207_192824.png

Re: How to debug JSON errors

Posted: 22 Sep 2022, 10:52
by Srbio
I have a problem I have made a plugin with night animations but
When I load the game it says that the id:Night Animation couldn't find an animation draft what should I do i have done everything right

Re: How to debug JSON errors

Posted: 22 Sep 2022, 11:23
by CommanderABab
May you share the code?

Re: How to debug JSON errors

Posted: 22 Sep 2022, 11:57
by Srbio
Plugin Error (T_T)

Unfortunately an error occurred while loading your plugins. Update or remove the plugin mentioned below in order to start Theo Town again. If you're a plugin creator you may have a deep look into your code (@.@).

A side note: You should add a plugin.manifest file to your plugin so that it can be identified when used by others.

In plugins..... my file ......json:

java.lang.IllegalStateException: When loading "...Also some file": Could not find an animation draft of id Night Animation!

Cancel