Convert string array to an array

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By kakcalu13

Please correct me if you caught something off.

So, the stored_value is from the UDP. The output in python: [989, 124, 518], [624, 310, 13]

Which is in another function:

func _process(delta):
    while socket.get_available_packet_count() > 0:
        data = socket.get_packet().get_string_from_utf8()
        stored_value = data

So, my question is that I simply can’t parse this following the code:

array_test = stored_value
        print(array_test)

        #total = array_test.size()
        var result_json: JSONParseResult = JSON.parse(stored_value) ##convert to json from string
        var parsedJson = result_json.get_result() # FIXED LINE
        print(parsedJson)

        var new_Json: String = to_json(parsedJson)
        print(new_Json)

The parse returns null. THe output is a string. I can’t convert it to an array nor anything. Do you know how to convert it where I can call it like new_Json[0][1] or new_json[1][0]?

What’s stored_value? If it’s literally [989, 124, 518], [624, 310, 13] then that’s not valid JSON. I assume get_result()is failing. What is the result_json error? I assume it’s EOF or a ‘key’ error. Something like this is valid JSON and works, but you need to decide on the structure yourself:

{
  "a": [989, 124, 518],
  "b": [624, 310, 13]
}

spaceyjase | 2021-11-22 21:11

This! Can you repeat that in the answer so I can mark this as an answer?

kakcalu13 | 2021-11-23 00:43

:bust_in_silhouette: Reply From: spaceyjase

As commented:

What’s stored_value? If it’s literally [989, 124, 518], [624, 310, 13] then that’s not valid JSON. I assume get_result()is failing. What is the result_json error? I assume it’s EOF or a ‘key’ error.

Something like this is valid JSON and works, but you need to decide on the structure yourself:

{
  "a": [989, 124, 518],
  "b": [624, 310, 13]
}

Hope that helps. Good luck!

This answered two of my posts which led to other post with also solved answer

For anyone who came here in future:
Here is what I did. I was using array in this post because dict didn’t work another day (in that same link) so I tested array and it results the same thing as dict. I made a mistake by using parse on array when it should be dict. I did not realize my dict output is not json configured properly. Here is the tool that helps you to confirm if it’s parsable or not. So I reverted my code back to other post to solve this issue.

If you are here for array only, here is what I did with an array only then I went back to dict after this post answered:
here is what I did to turn the output into 1D.

if stored_value != "":
		var array_test = stored_value.replace("[", "")
		array_test = array_test.replace("]", "")
		test=array_test.split(",", true, '0')

Basically, stored_value gets output like this:

[[5, 6, 1], [8, 3, 7], [4, 4, 3], [1, 1, 6], [9, 9, 7], [2, 9, 3], [3, 2, 7], [4, 5, 1], [9, 3, 2], [8, 5, 6], [4, 4, 5], [4, 6, 2], [2, 4, 9], [9, 2, 3], [1, 4, 4], [7, 6, 1], [1, 9, 3], [9, 7, 2], [3, 8, 6], [4, 8, 2], [7, 9, 9], [4, 6, 4], [1, 4, 6], [2, 9, 7], [3, 6, 2], [1, 9, 5], [3, 4, 4], [7, 2, 5], [8, 9, 1], [3, 8, 1], [7, 6, 5], [8, 2, 5], [8, 7, 1], [5, 5, 4], [6, 9, 8], [9, 9, 6], [6, 6, 5], [6, 3, 6], [9, 9, 8], [5, 1, 2], [7, 6, 6], [8, 8, 7], [3, 3, 9], [6, 1, 8], [4, 5, 2], [6, 9, 3], [1, 5, 7], [9, 3, 6], [1, 7, 4], [5, 5, 8], [3, 7, 6], [2, 4, 1], [8, 2, 2], [5, 7, 8], [9, 7, 3], [1, 9, 4], [8, 6, 2], [5, 4, 3], [2, 1, 7], [2, 2, 6], [4, 1, 9], [2, 6, 6], [5, 8, 5], [4, 4, 4], [9, 5, 1], [6, 7, 3], [5, 5, 5], [9, 3, 3], [4, 2, 3], [4, 6, 3], [7, 2, 2], [1, 1, 9], [9, 5, 3], [8, 9, 7], [1, 8, 5], [6, 9, 2], [7, 4, 2], [5, 4, 9], [7, 2, 4], [1, 4, 7], [2, 8, 9], [8, 9, 9], [2, 3, 7], [8, 7, 9], [6, 3, 3], [3, 1, 6], [3, 3, 3], [9, 5, 7], [6, 1, 5], [3, 8, 2], [1, 8, 9], [9, 6, 8]] 

to this processed output:

[5, 6, 1, 8, 3, 7, 4, 4, 3, 1, 1, 6, 9, 9, 7, 2, 9, 3, 3, 2, 7, 4, 5, 1, 9, 3, 2, 8, 5, 6, 4, 4, 5, 4, 6, 2, 2, 4, 9, 9, 2, 3, 1, 4, 4, 7, 6, 1, 1, 9, 3, 9, 7, 2, 3, 8, 6, 4, 8, 2, 7, 9, 9, 4, 6, 4, 1, 4, 6, 2, 9, 7, 3, 6, 2, 1, 9, 5, 3, 4, 4, 7, 2, 5, 8, 9, 1, 3, 8, 1, 7, 6, 5, 8, 2, 5, 8, 7, 1, 5, 5, 4, 6, 9, 8, 9, 9, 6, 6, 6, 5, 6, 3, 6, 9, 9, 8, 5, 1, 2, 7, 6, 6, 8, 8, 7, 3, 3, 9, 6, 1, 8, 4, 5, 2, 6, 9, 3, 1, 5, 7, 9, 3, 6, 1, 7, 4, 5, 5, 8, 3, 7, 6, 2, 4, 1, 8, 2, 2, 5, 7, 8, 9, 7, 3, 1, 9, 4, 8, 6, 2, 5, 4, 3, 2, 1, 7, 2, 2, 6, 4, 1, 9, 2, 6, 6, 5, 8, 5, 4, 4, 4, 9, 5, 1, 6, 7, 3, 5, 5, 5, 9, 3, 3, 4, 2, 3, 4, 6, 3, 7, 2, 2, 1, 1, 9, 9, 5, 3, 8, 9, 7, 1, 8, 5, 6, 9, 2, 7, 4, 2, 5, 4, 9, 7, 2, 4, 1, 4, 7, 2, 8, 9, 8, 9, 9, 2, 3, 7, 8, 7, 9, 6, 3, 3, 3, 1, 6, 3, 3, 3, 9, 5, 7, 6, 1, 5, 3, 8, 2, 1, 8, 9, 9, 6, 8]

So, I did something like this to generate x,y,z:

			while key < total:
			if flag == 0:
				flag = flag + 1
				x = int(test[key])
			elif flag == 1:
				flag = flag + 1
				y = int(test[key])
			elif flag == 2:
				flag = 0
				z = int(test[key]) 
				$GridMap.set_cell_item(x,y,z, 0)

Obv, you will have your own goal. This is only example for you to get the idea. Hopefully, you have a better idea than this.

By the time you come to here, I’m probably more experience by that time (approx 3 months from now on) so feel free to ask or correct me (if you caught something off so you can educate others and me)

Spacejase, thank you for your time!!!

kakcalu13 | 2021-11-23 15:34