Convert string dict to dict

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

So, I got the string dict from python3 through UDP like this:

[{"number": 0, "x": 888, "y": 952, "z": 142}, {"number": 1, "x": 696, "y": 404, "z": 607}, {"number": 2, "x": 982, "y": 954, "z": 541}, {"number": 3, "x": 578, "y": 948, "z": 407}, {"number": 4, "x": 687, "y": 899, "z": 362}, {"number": 5, "x": 392, "y": 1, "z": 585}, {"number": 6, "x": 547, "y": 367, "z": 119}, {"number": 7, "x": 715, "y": 463, "z": 838}, {"number": 8, "x": 321, "y": 322, "z": 30}, {"number": 9, "x": 220, "y": 119, "z": 899}, {"number": 10, "x": 947, "y": 494, "z": 265}, {"number": 11, "x": 502, "y": 346, "z": 149}, {"number": 12, "x": 34, "y": 218, "z": 320}, {"number": 13, "x": 296, "y": 532, "z": 241}, {"number": 14, "x": 610, "y": 391, "z": 271}, {"number": 15, "x": 293, "y": 424, "z": 956}, {"number": 16, "x": 765, "y": 615, "z": 553}, {"number": 17, "x": 998, "y": 793, "z": 944}, {"number": 18, "x": 199, "y": 273, "z": 988}, {"number": 19, "x": 745, "y": 649, "z": 614}, {"number": 20, "x": 122, "y": 737, "z": 599}, {"number": 21, "x": 183, "y": 799, "z": 81}, {"number": 22, "x": 766, "y": 401, "z": 378}, {"number": 23, "x": 731, "y": 137, "z": 33}, {"number": 24, "x": 773, "y": 983, "z": 374}, {"number": 25, "x": 885, "y": 412, "z": 794}, {"number": 26, "x": 795, "y": 502, "z": 166}, {"number": 27, "x": 737, "y": 158, "z": 562}, {"number": 28, "x": 215, "y": 703, "z": 805}, {"number": 29, "x": 644, "y": 483, "z": 98}, {"number": 30, "x": 955, "y": 535, "z": 981}, {"number": 31, "x": 299, "y": 607, "z": 413}, {"number": 32, "x": 289, "y": 666, "z": 333}, {"number": 33, "x": 179, "y": 34, "z": 930}, {"number": 34, "x": 432, "y": 350, "z": 861}, {"number": 35, "x": 824, "y": 934, "z": 694}, {"number": 36, "x": 429, "y": 486, "z": 817}, {"number": 37, "x": 442, "y": 100, "z": 364}, {"number": 38, "x": 72, "y": 47, "z": 985}, {"number": 39, "x": 254, "y": 339, "z": 615}, {"number": 40, "x": 86, "y": 681, "z": 794}, {"number": 41, "x": 841, "y": 232, "z": 364}, {"number": 42, "x": 691, "y": 569, "z": 114}, {"number": 43, "x": 20, "y": 818, "z": 72}, {"number": 44, "x": 973, "y": 110, "z": 733}, {"number": 45, "x": 595, "y": 326, "z": 553}, {"number": 46, "x": 721, "y": 159, "z": 143}, {"number": 47, "x": 579, "y": 824, "z": 815}, {"number": 48, "x": 592, "y": 237, "z": 233}, {"number": 49, "x": 165, "y": 436, "z": 800}, {"number": 50, "x": 422, "y": 382, "z": 845}, {"number": 51, "x": 559, "y": 34, "z": 876}, {"number": 52, "x": 908, "y": 234, "z": 11}, {"number": 53, "x": 301, "y": 418, "z": 306}, {"number": 54, "x": 792, "y": 978, "z": 100}, {"number": 55, "x": 546, "y": 274, "z": 715}, {"number": 56, "x": 324, "y": 711, "z": 916}, {"number": 57, "x": 178, "y": 746, "z": 213}, {"number": 58, "x": 382, "y": 124, "z": 683}, {"number": 59, "x": 886, "y": 51, "z": 813}, {"number": 60, "x": 775, "y": 509, "z": 449}, {"number": 61, "x": 212, "y": 349, "z": 829}, {"number": 62, "x": 925, "y": 281, "z": 335}, {"number": 63, "x": 689, "y": 381, "z": 72}, {"number": 64, "x": 599, "y": 312, "z": 781}, {"number": 65, "x": 734, "y": 66, "z": 821}, {"number": 66, "x": 423, "y": 285, "z": 493}, {"number": 67, "x": 939, "y": 281, "z": 63}, {"number": 68, "x": 485, "y": 221, "z": 130}, {"number": 69, "x": 821, "y": 855, "z": 934}, {"number": 70, "x": 545, "y": 167, "z": 473}, {"number": 71, "x": 314, "y": 871, "z": 702}, {"number": 72, "x": 626, "y": 94, "z": 588}, {"number": 73, "x": 942, "y": 453, "z": 796}, {"number": 74, "x": 538, "y": 205, "z": 419}, {"number": 75, "x": 508, "y": 468, "z": 500}, {"number": 76, "x": 346, "y": 585, "z": 7}, {"number": 77, "x": 396, "y": 900, "z": 232}, {"number": 78, "x": 852, "y": 617, "z": 350}, {"number": 79, "x": 687, "y": 63, "z": 4}, {"number": 80, "x": 723, "y": 565, "z": 273}, {"number": 81, "x": 628, "y": 303, "z": 284}, {"number": 82, "x": 730, "y": 373, "z": 245}, {"number": 83, "x": 762, "y": 168, "z": 728}, {"number": 84, "x": 373, "y": 884, "z": 517}, {"number": 85, "x": 974, "y": 743, "z": 208}, {"number": 86, "x": 372, "y": 721, "z": 689}, {"number": 87, "x": 78, "y": 2, "z": 269}, {"number": 88, "x": 636, "y": 615, "z": 774}, {"number": 89, "x": 559, "y": 38, "z": 797}, {"number": 90, "x": 486, "y": 179, "z": 588}, {"number": 91, "x": 643, "y": 238, "z": 513}, {"number": 92, "x": 799, "y": 518, "z": 181}, {"number": 93, "x": 341, "y": 529, "z": 59}, {"number": 94, "x": 21, "y": 113, "z": 364}, {"number": 95, "x": 77, "y": 784, "z": 255}, {"number": 96, "x": 191, "y": 58, "z": 448}, {"number": 97, "x": 613, "y": 522, "z": 539}, {"number": 98, "x": 911, "y": 772, "z": 262}, {"number": 99, "x": 302, "y": 290, "z": 109}]

I was trying to convert string to dict inside gd.

So I tried examples like

var json := to_json(stored_value) ##stored_value gets from UDP
	var p = JSON.parse(json)
	if typeof(p.result) == TYPE_ARRAY:
		print(p.result[0]) # prints 'hello'
	else:
		print("unexpected results")
	print(value)

The output is:
unexpected results

I tried other thing:

	value = parse_json(str(stored_value)).result
	print(value)

error: Invalid get index 'result' (on base: 'Dictionary').

How do you convert the string dict to dict in godot where I can update data automatically by python3.

This one, I did with a hardcode (trying to avoid that) which it worked

var value=[{"number": 0, "x": 888, "y": 952, "z": 142}, {"number": 1, "x": 696, "y": 404, "z": 607}, {"number": 2, "x": 982, "y": 954, "z": 541}, {"number": 3, "x": 578, "y": 948, "z": 407}, {"number": 4, "x": 687, "y": 899, "z": 362}, {"number": 5, "x": 392, "y": 1, "z": 585}, {"number": 6, "x": 547, "y": 367, "z": 119}, {"number": 7, "x": 715, "y": 463, "z": 838}, {"number": 8, "x": 321, "y": 322, "z": 30}, {"number": 9, "x": 220, "y": 119, "z": 899}, {"number": 10, "x": 947, "y": 494, "z": 265}, {"number": 11, "x": 502, "y": 346, "z": 149}, {"number": 12, "x": 34, "y": 218, "z": 320}, {"number": 13, "x": 296, "y": 532, "z": 241}, {"number": 14, "x": 610, "y": 391, "z": 271}, {"number": 15, "x": 293, "y": 424, "z": 956}, {"number": 16, "x": 765, "y": 615, "z": 553}, {"number": 17, "x": 998, "y": 793, "z": 944}, {"number": 18, "x": 199, "y": 273, "z": 988}, {"number": 19, "x": 745, "y": 649, "z": 614}, {"number": 20, "x": 122, "y": 737, "z": 599}, {"number": 21, "x": 183, "y": 799, "z": 81}, {"number": 22, "x": 766, "y": 401, "z": 378}, {"number": 23, "x": 731, "y": 137, "z": 33}, {"number": 24, "x": 773, "y": 983, "z": 374}, {"number": 25, "x": 885, "y": 412, "z": 794}, {"number": 26, "x": 795, "y": 502, "z": 166}, {"number": 27, "x": 737, "y": 158, "z": 562}, {"number": 28, "x": 215, "y": 703, "z": 805}, {"number": 29, "x": 644, "y": 483, "z": 98}, {"number": 30, "x": 955, "y": 535, "z": 981}, {"number": 31, "x": 299, "y": 607, "z": 413}, {"number": 32, "x": 289, "y": 666, "z": 333}, {"number": 33, "x": 179, "y": 34, "z": 930}, {"number": 34, "x": 432, "y": 350, "z": 861}, {"number": 35, "x": 824, "y": 934, "z": 694}, {"number": 36, "x": 429, "y": 486, "z": 817}, {"number": 37, "x": 442, "y": 100, "z": 364}, {"number": 38, "x": 72, "y": 47, "z": 985}, {"number": 39, "x": 254, "y": 339, "z": 615}, {"number": 40, "x": 86, "y": 681, "z": 794}, {"number": 41, "x": 841, "y": 232, "z": 364}, {"number": 42, "x": 691, "y": 569, "z": 114}, {"number": 43, "x": 20, "y": 818, "z": 72}, {"number": 44, "x": 973, "y": 110, "z": 733}, {"number": 45, "x": 595, "y": 326, "z": 553}, {"number": 46, "x": 721, "y": 159, "z": 143}, {"number": 47, "x": 579, "y": 824, "z": 815}, {"number": 48, "x": 592, "y": 237, "z": 233}, {"number": 49, "x": 165, "y": 436, "z": 800}, {"number": 50, "x": 422, "y": 382, "z": 845}, {"number": 51, "x": 559, "y": 34, "z": 876}, {"number": 52, "x": 908, "y": 234, "z": 11}, {"number": 53, "x": 301, "y": 418, "z": 306}, {"number": 54, "x": 792, "y": 978, "z": 100}, {"number": 55, "x": 546, "y": 274, "z": 715}, {"number": 56, "x": 324, "y": 711, "z": 916}, {"number": 57, "x": 178, "y": 746, "z": 213}, {"number": 58, "x": 382, "y": 124, "z": 683}, {"number": 59, "x": 886, "y": 51, "z": 813}, {"number": 60, "x": 775, "y": 509, "z": 449}, {"number": 61, "x": 212, "y": 349, "z": 829}, {"number": 62, "x": 925, "y": 281, "z": 335}, {"number": 63, "x": 689, "y": 381, "z": 72}, {"number": 64, "x": 599, "y": 312, "z": 781}, {"number": 65, "x": 734, "y": 66, "z": 821}, {"number": 66, "x": 423, "y": 285, "z": 493}, {"number": 67, "x": 939, "y": 281, "z": 63}, {"number": 68, "x": 485, "y": 221, "z": 130}, {"number": 69, "x": 821, "y": 855, "z": 934}, {"number": 70, "x": 545, "y": 167, "z": 473}, {"number": 71, "x": 314, "y": 871, "z": 702}, {"number": 72, "x": 626, "y": 94, "z": 588}, {"number": 73, "x": 942, "y": 453, "z": 796}, {"number": 74, "x": 538, "y": 205, "z": 419}, {"number": 75, "x": 508, "y": 468, "z": 500}, {"number": 76, "x": 346, "y": 585, "z": 7}, {"number": 77, "x": 396, "y": 900, "z": 232}, {"number": 78, "x": 852, "y": 617, "z": 350}, {"number": 79, "x": 687, "y": 63, "z": 4}, {"number": 80, "x": 723, "y": 565, "z": 273}, {"number": 81, "x": 628, "y": 303, "z": 284}, {"number": 82, "x": 730, "y": 373, "z": 245}, {"number": 83, "x": 762, "y": 168, "z": 728}, {"number": 84, "x": 373, "y": 884, "z": 517}, {"number": 85, "x": 974, "y": 743, "z": 208}, {"number": 86, "x": 372, "y": 721, "z": 689}, {"number": 87, "x": 78, "y": 2, "z": 269}, {"number": 88, "x": 636, "y": 615, "z": 774}, {"number": 89, "x": 559, "y": 38, "z": 797}, {"number": 90, "x": 486, "y": 179, "z": 588}, {"number": 91, "x": 643, "y": 238, "z": 513}, {"number": 92, "x": 799, "y": 518, "z": 181}, {"number": 93, "x": 341, "y": 529, "z": 59}, {"number": 94, "x": 21, "y": 113, "z": 364}, {"number": 95, "x": 77, "y": 784, "z": 255}, {"number": 96, "x": 191, "y": 58, "z": 448}, {"number": 97, "x": 613, "y": 522, "z": 539}, {"number": 98, "x": 911, "y": 772, "z": 262}, {"number": 99, "x": 302, "y": 290, "z": 109}]
total = value.size()
	for key in total:
		x = (value[key].x)/100
		y = (value[key].y)/100
		z = (value[key].z)/100
		current_pos = Vector3(x,y,z)
		$GridMap.set_cell_item(current_pos.x, current_pos.y, current_pos.z, 0)

Only this worked because hardcode AND simply dict. I struggled to convert a string dict from either json file or UDP. I spent almost 4 hours on this trying like 10+ examples.

:bust_in_silhouette: Reply From: dewcked

JSON.parse returns JSONParseResult that inherit Object type(Variant.Type value = 17) which is differnt from Array type(Variant.Type value = 19) and Dictionary type(Variant.Type Value = 18).

So, you must use this JSONParseResult’s inner method → get_result()

var parsedJson: JSONParseResult = JSON.parse('JSON HERE')
var Array_or_Dictionary = parsedJson.get_result()

then the result type is Array or Dictionary. You can search it from docs when it returns Array and When returns Dictionary.

It’s done! Now you can use it :smiley:

Thank you so much for your reply! It means a lot to me.

I added your solution to this code:

var result_json = JSON.parse(stored_value) ##convert to json from string
		stored_value = result_json #put json dict to stored_value dict
		var json := to_json(stored_value) #convert to object
		var parsedJson : JSONParseResult = JSON.parse(stored_value)
		var Array_or_Dictionary = parsedJson.get_result()

At first, the error was like
Invalid type in function 'parse' in base '_JSON'. Cannot convert argument 1 from Dictionary to String.

So I updated the variable from this to this:
var stored_value = {}
to
var stored_value = ""

Unfortunately, I got the error
Invalid type in function 'parse' in base '_JSON'. Cannot convert argument 1 from Object to String.

I have a feeling that it was me doing something off. Did you see anything off from this one?

P.S. how did you get the black format thing on the code?

kakcalu13 | 2021-11-19 04:03

Example

var stored_value = """[{"number": 0, "x": 888, "y": 952, "z": 142}, {"number": 1, "x": 696, "y": 404, "z": 607}]"""

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)

This will change json string format to Dictionary, and Dictionary to json string format again.

dewcked | 2021-11-19 04:34

Thank you so much for your time!

It returns null :frowning:

I added few prints. I will post each output

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

I will comment each print and uncomment each print’s turn

  1. print(stored_value)output:
    [{‘number’: 0, ‘x’: 582, ‘y’: 729, ‘z’: 379}, {‘number’: 1, ‘x’: 208, ‘y’: 588, ‘z’: 489}, {‘number’: 2, ‘x’: 778, ‘y’: 869, ‘z’: 477}, {‘number’: 3, ‘x’: 123, ‘y’: 447, ‘z’: 373}, {‘number’: 4, ‘x’: 795, ‘y’: 280, ‘z’: 802}, {‘number’: 5, ‘x’: 60, ‘y’: 140, ‘z’: 64}, {‘number’: 6, ‘x’: 526, ‘y’: 673, ‘z’: 319}, {‘number’: 7, ‘x’: 390, ‘y’: 593, ‘z’: 83}, {‘number’: 8, ‘x’: 638, ‘y’: 618, ‘z’: 332}, {‘number’: 9, ‘x’: 565, ‘y’: 122, ‘z’: 661}, {‘number’: 10, ‘x’: 318, ‘y’: 817, ‘z’: 671}, {‘number’: 11, ‘x’: 472, ‘y’: 423, ‘z’: 170}, {‘number’: 12, ‘x’: 3, ‘y’: 639, ‘z’: 978}, {‘number’: 13, ‘x’: 410, ‘y’: 211, ‘z’: 328}, {‘number’: 14, ‘x’: 969, ‘y’: 517, ‘z’: 364}, {‘number’: 15, ‘x’: 397, ‘y’: 313, ‘z’: 297}, {‘number’: 16, ‘x’: 199, ‘y’: 584, ‘z’: 327}, {‘number’: 17, ‘x’: 589, ‘y’: 289, ‘z’: 435}, {‘number’: 18, ‘x’: 321, ‘y’: 733, ‘z’: 819}, {‘number’: 19, ‘x’: 541, ‘y’: 91, ‘z’: 191}, {‘number’: 20, ‘x’: 814, ‘y’: 198, ‘z’: 249}, {‘number’: 21, ‘x’: 117, ‘y’: 687, ‘z’: 737}, {‘number’: 22, ‘x’: 449, ‘y’: 918, ‘z’: 680}, {‘number’: 23, ‘x’: 639, ‘y’: 400, ‘z’: 438}, {‘number’: 24, ‘x’: 423, ‘y’: 470, ‘z’: 220}, {‘number’: 25, ‘x’: 931, ‘y’: 956, ‘z’: 983}, {‘number’: 26, ‘x’: 172, ‘y’: 503, ‘z’: 623}, {‘number’: 27, ‘x’: 73, ‘y’: 492, ‘z’: 264}, {‘number’: 28, ‘x’: 177, ‘y’: 280, ‘z’: 429}, {‘number’: 29, ‘x’: 232, ‘y’: 761, ‘z’: 877}, {‘number’: 30, ‘x’: 428, ‘y’: 872, ‘z’: 928}, {‘number’: 31, ‘x’: 423, ‘y’: 669, ‘z’: 886}, {‘number’: 32, ‘x’: 462, ‘y’: 410, ‘z’: 769}, {‘number’: 33, ‘x’: 647, ‘y’: 769, ‘z’: 155}, {‘number’: 34, ‘x’: 391, ‘y’: 155, ‘z’: 667}, {‘number’: 35, ‘x’: 209, ‘y’: 365, ‘z’: 15}, {‘number’: 36, ‘x’: 410, ‘y’: 588, ‘z’: 479}, {‘number’: 37, ‘x’: 590, ‘y’: 513, ‘z’: 340}, {‘number’: 38, ‘x’: 753, ‘y’: 817, ‘z’: 29}, {‘number’: 39, ‘x’: 935, ‘y’: 955, ‘z’: 421}[…]
    [output overflow, print less text!]

Which is expected.

  1. print(result_json) output:

[JSONParseResult:1295]
[JSONParseResult:1322]
[JSONParseResult:1358]
[JSONParseResult:1361]
[JSONParseResult:1364]
[JSONParseResult:1367]
[JSONParseResult:1370]

Which should be expected?

  1. print(parsedJson) output:
    Null
    Null
    Null
    Null
    Null
    Null
    Null
    Null
    Null
    Null
    Null

Do you think it could be bug? From what I see on your code, it looks very promising!

kakcalu13 | 2021-11-19 05:30

I’m using godot3.4 So, it might be different.

I tested this case.

First, I put Json file named “tesing.json” into

C:\Users\USER-\AppData\Roaming\Godot\app_userdata\[ProjectName]

as I use WindowsOS. After that, put this code there.

func test():
    var file = File.new()
    if file.open("user://testing.json", File.READ) != 0:
        return
    var stored_value: String = file.get_as_text()
    file.close()
    var result_json = JSON.parse(stored_value)
    var parsedJson = result_json.get_result()
    print(parsedJson[0])
    print(parsedJson[1])

This prints out nicely. If it prints null, check JSON syntax in the file.

dewcked | 2021-11-19 06:55

Ah, gotcha! Json seems ok.

The output from UDP is:

{'number': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99], 'x': [768, 782, 575, 348, 828, 776, 390, 259, 922, 114, 928, 671, 23, 923, 600, 436, 592, 117, 678, 476, 196, 9, 771, 2, 191, 961, 728, 746, 407, 577, 311, 348, 823, 205, 983, 571, 719, 348, 695, 956, 156, 392, 978, 969, 278, 193, 322, 294, 351, 171, 994, 525, 810, 55, 704, 192, 197, 163, 111, 345, 944, 421, 141, 943, 565, 648, 950, 188, 641, 165, 606, 950, 594, 626, 562, 810, 905, 697, 718, 497, 196, 181, 523, 136, 212, 256, 761, 619, 641, 931, 559, 561, 720, 132, 132, 460, 321, 907, 714, 794], 'y': [827, 502, 291, 22, 686, 537, 533, 739, 69, 479, 504, 848, 912, 928, 819, 91, 247, 961, 338, 20, 428, 893, 878, 644, 898, 582, 554, 541, 561, 306, 643, 565, 160, 566, 813, 386, 50, 532, 394, 746, 113, 373, 232, 907, 28, 887, 726, 997, 154, 458, 834, 190, 577, 734, 856, 935, 953, 530, 661, 884, 899, 490, 685, 909, 688, 826, 242, 81, 522, 566, 474, 715, 945, 17, 851, 409, 193, 559, 670, 678, 306, 390, 576, 508, 934, 997, 778, 64, 376, 733, 19, 373, 799, 737, 857, 31, 172, 114, 705, 936], 'z': [811, 48, 855, 887, 578, 394, 77, 552, 444, 384, 109, 381, 279, 473, 415, 747, 187, 437, 18, 752, 686, 418, 882, 886, 771, 989, 67, 158, 10, 90, 316, 101, 964, 32, 122, 281, 913, 790, 353, 187, 210, 517, 123, 740, 7, 442, 188, 221, 223, 360, 411, 123, 837, 806, 662, 831, 174, 679, 328, 1, 141, 268, 662, 656, 889, 152, 563, 110, 315, 826, 372, 467, 887, 642, 262, 871, 796, 852, 992, 215, 994, 28, 259, 372, 537, 689, 630, 23, 384, 628, 123, 8, 366, 94, 95, 851, 305, 977, 15, 426]}

The output JUST updated btw. It is still returning null

I guess I just gotta download latest one asap

kakcalu13 | 2021-11-19 10:09

You were right all the time. I missed the json error on my end completely.

Mentioned this post from other post

To anyone in future who came here:
Please read the link and if you still have a question, feel free to comment on here so I can answer you. I should be more experience by the time you come here.

Dewcked, thank you so much for your time!

kakcalu13 | 2021-11-23 15:37