[Solved] Need Help with "Can't parse JSON" Error

I’m encountering an issue in my Roblox script, and I could really use some assistance. I keep getting a “Can’t parse JSON” error, and despite trying various solutions, I haven’t been able to resolve it, I want it to print in a table form.

Here’s the script I’m working with:



local success, response = pcall(function()
	return [[
[
  {
    "city": "חולון",
    "area": "דן",
    "countdown": "דקה וחצי",
    "id": 2135088806179848000
  }
],
[
  {
    "city": "תל אביב - מזרח",
    "area": "דן",
    "countdown": "דקה וחצי",
    "id": 2135088806179848000
  }
],
[
  {
    "city": "אשקלון - צפון",
    "area": "מערב לכיש",
    "countdown": "30 שניות",
    "id": 2135088806179848000
  }
]
]]
end)

if success then
	local jsonData = HttpService:JSONDecode(response)
	print(jsonData)
end

I’ve tried various methods, but the “Can’t parse JSON” error persists. If anyone can help me identify the issue and provide a solution, I would greatly appreciate it.

Thank you in advance for your assistance!

4 Likes

what does the JSON table look like?

6 Likes

Hey! We can help you better if you can provide us with the actual Table that you’re trying to parse. (response). print(response) instead of trying to decode it directly and show us

2 Likes

A good tip for resolving JSON issues is using a JSON validator. Paste your JSON table into one and it’ll highlight any syntax issues.

For example: https://jsonlint.com

2 Likes

I just used Postman to perform GET on the mentioned API in your code. The issue is the API your hitting is returning invalid JSON.

It is currently responding with:

Correct JSON for this would look like this:

If this is your API you’re using, check you’re correctly structuring your JSON payload. Perhaps put it through a JSONIFY function (dependant on which back-end language you’re using).

3 Likes

{
“city”: “חולון”,
“area”: “דן”,
“countdown”: “דקה וחצי”,
“id”: 3520004751503131600
}
{
“city”: “תל אביב - מזרח”,
“area”: “דן”,
“countdown”: “דקה וחצי”,
“id”: 3520004751503131600
}
{
“city”: “אשקלון - צפון”,
“area”: “מערב לכיש”,
“countdown”: “30 שניות”,
“id”: 3520004751503131600
}

cant get the API to look like that, any idea how to do it in the lua code itself?

You’d have to create your own function to decode in this instance then, as it’s not valid JSON.

i tried my harder but I ouldnt do it, csn you try?

local HttpService = game:GetService("HttpService")
local apiUrl = "https://exclude-reward-planners-arthur.trycloudflare.com/redv2"

local success, response = pcall(function()
	return HttpService:GetAsync(apiUrl)
end)

if success then
	response = response:gsub(']%s*%[', ', ')
	local jsonData = HttpService:JSONDecode(response)
end

1 Like

In order to get an understanding of what the above does, here are some resources so you can develop your skills. (:

1 Like

Thank you for mentioning these resources, I had to run so couldn’t really give any insight to string formatting ahs

2 Likes

did u ever try turning the json into a lua table then use httpservice to encode it when u send it to the server?

Yes?.. why do you ask? I’ve done that for sending requests on my own APIs in the past for sure. Encoding to send to the server to save and Decoding data received from it.

I’m pretty sure OP is trying to get data from an external API that they have no control over (formatting wise)

oops i replied to the wrong person, my bad

you need to put a “,” after every other tablebefore.

{
{
“city”: “חולון”,
“area”: “דן”,
“countdown”: “דקה וחצי”,
“id”: 3520004751503131600
},
{
“city”: “תל אביב - מזרח”,
“area”: “דן”,
“countdown”: “דקה וחצי”,
“id”: 3520004751503131600
},
{
“city”: “אשקלון - צפון”,
“area”: “מערב לכיש”,
“countdown”: “30 שניות”,
“id”: 3520004751503131600
}
}

Or you can reconstruct your json tables to be like this

{
	"City": {
		"area": "...",
		"countdown": "...",
		"id": ...
	},
	"City": {
		"area": "...",
		"countdown": "...",
		"id": ...
	}
}

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.