Iterating through JSON string

Hello there!, I’m Steve

  1. What do you want to achieve? I want to iterate through a JSON string.

  2. What is the issue? I get an error while trying to do so, ‘invalid argument #1 to ‘pairs’ (table expected, got string)’, Although the output doesn’t display it as a string (no quotation marks at start of end)

  3. What solutions have you tried so far? none, I don’t have any ideas on how I would be able to fix this.

Script:

local HttpService = game:GetService("HttpService")
local lowestping = 9999999999999999999999
local response_table = {}

local url = "(server url)"
local dataFields = {
	["gameid"] = (game id number)
}

local response = HttpService:RequestAsync(
	{
		Url = url,
		Method = "POST",
		Headers = {
			["Content-Type"] = "application/json"
		},
		Body = HttpService:JSONEncode(dataFields)
	}
)


if response.Success then
	local end_result = HttpService:JSONDecode(response.Body)
	
	print("Status code:", response.StatusCode, response.StatusMessage)
	
	print(end_result) -- The output looks something like {"test": true} (because my server returns that)
	
	for _,v in pairs(end_result) do -- The error appears here
		if v.ping <= lowestping then
			lowestping = v.ping
			print(lowestping)
		end

		wait()
	end

	print(lowestping)
end

My server uses application/json ((bodyParser.json()) <= nodejs)

1 Like

Have you tried Debugging | Roblox Creator Documentation to examine the variables and make sure they’re what you expect?

My guess is that your server is actually returning something like

"{\"test\": true}"

And so you’re parsing it into a string rather than a JSON object.

1 Like

I’m gonna try doing that, thanks.

Maybe you can solve it with: HttpService | Roblox Creator Documentation

Let’s pretend I don’t have negative IQ. (Sorry lol)

1 Like

My script has that already, @nicemike40, my server is returning that, but i’m decoding it using JSONDecode

1 Like

Here’s my server code if you’re wondering

var express = require('express');
var fetch = require("node-fetch")
var bodyParser = require('body-parser')

var app = express();

var jsonParser = bodyParser.json()

app.post('/', jsonParser, function (req, res) {
  res.json({"test": true})
})

app.listen(3000, () => {
  console.log("Server started.");
});
1 Like

can you show us what it actually prints instead of just an example please?
just censor anything sensitive if there’s anything.

1 Like

Well if your server is sending the literal characters "{\"test\": true}", that’s an issue, because it will be parsed as a single string.

It should be sending the characters {"test": true}. But it looks like your express code does that so might not be the issue anyways. But just something to check with the debugger.

1 Like

The server script returns a JSON object that has a server list of a game using roblox’s api

Here’s what it prints out: pastebin.com/raw/ECE56GC8

Maybe JSONDecode doesn’t work for JavaScript (JSON/Table)'s?, I don’t really know.

Can you try doing another JSONDecode just to see?

local end_result = (decode blah blah blah)
end_result = HttpService:JSONDecode(end_result)

and try again?


Well that’s um… weird?

1 Like

I don’t know how, but it worked, Thank you so much.

1 Like

it’s almost definitely because your server is sending what I said

So your code gets

"{\"test\": true}" (a string)

One decode gives you

{"test": true} (still a string)

The second decode gives you

{test = true} (finally a table)

In other words, the root cause is probably your server.

1 Like