Hey all, I'm having trouble finding how to turn a string back into an array. I've got an array of Vector3s that I'm sending over the network via JSON string (since the array has a variable length) so I need to turn it back into an array of Vector3 on the other end of the network. I can make the JSON string easily, and send it easily, all the problems are on the other end.

func test_json(json: String):
var new_array = JSON.new()

I've tried a few things from here, but nothing seems to be working right. I know I need to create a variable that is an array, but how to fill that array I can't seem to find the appropriate code. Any help?

    CorvaNocta

    https://docs.godotengine.org/en/stable/classes/class_json.html

    # Retrieve data
    var json = JSON.new()
    var error = json.parse(json_string)
    if error == OK:
    	var data_received = json.data
    	if typeof(data_received) == TYPE_ARRAY:
    		print(data_received) # Prints array
    	else:
    		print("Unexpected data")
    else:
    	print("JSON Parse Error: ", json.get_error_message(), " in ", json_string, " at line ", json.get_error_line())

    what exactly are you having trouble with?

    data is a Variant, it can be whatever. if your JSON is JUST an array of vector3, then that's it.
    if it's a dictionary, first print it, and then you can access one of the values by key.

    you can check the variant types in here:
    https://docs.godotengine.org/en/stable/classes/class_%40globalscope.html#enum-globalscope-variant-type

      Jesusemora I must be typing in something wrong, I found that code on the wiki but it just returned "0" for me. I'll try it again

      CorvaNocta try printing the array before turning it into a JSON, and print the data of the JSON after parsing.
      it will tell you:
      what is the type that is being used
      if the array has any data at all
      what the data looks like after parsing

        Jesusemora so the string that is coming in from the network is this:

        "[(15.5, 0.875, -13.5), (16.5, 0.875, -13.5), (17.5, 0.875, -13.5)]"

        and after parsing it looks like this:

        [(15.5, 0.875, -13.5), (16.5, 0.875, -13.5), (17.5, 0.875, -13.5)]

        but if I try to print just the first entry, I get this:

        [

          CorvaNocta I tried your String and it throws error 43.
          are you in godot 4?

          after adding additional "s, it looks like this:
          "\"[(15.5, 0.875, -13.5), (16.5, 0.875, -13.5), (17.5, 0.875, -13.5)]\""
          and it works.

          the stringified Array should look like this:
          ["(15.5, 0.875, -13.5)", "(16.5, 0.875, -13.5)", "(17.5, 0.875, -13.5)"]

          I tried with a PackedVector3 and it looks like this:
          "[(0, 0, 0), (1, 0, 1), (0, 1, 0)]"
          and it's a string, like it cannot be parsed by JSON.
          so maybe that's your problem.

          use a conventional array.

            Jesusemora Yeah I'm using 4.

            I got it to look like that once, with the \ around the entries, but I don't remember how I got there.

            This is what I am sending:

            var string_path = JSON.stringify(path)

            which looks like this when I print it:

            "[(17.5, 0.875, -13.5), (16.5, 0.875, -13.5)]"

            so its sending this string across the network. But if it can't be a string, how do I send it as an array? just literally make it ARRAY?

              CorvaNocta This is what I am sending:

              var string_path = JSON.stringify(path)

              which looks like this when I print it:

              "[(17.5, 0.875, -13.5), (16.5, 0.875, -13.5)]"

              that won't parse. what is path? it should be an Array. if it's a PackedVector3Array it fails.

              what I meant and maybe didn't explain well, is that when you convert an Array of Vector3 to a json string, it should have the " between each Vector3, like this:

              ["(15.5, 0.875, -13.5)", "(16.5, 0.875, -13.5)", "(17.5, 0.875, -13.5)"]

              CorvaNocta oh, I CAN just send an array lol. I didn't know I could do that

              well... good... you found a solution. I didn't mean that when I said conventional array, I meant a conventional array stringified instead of a packedVector3Array... but if it works it works 😆