parsing a json file into multiple dictionaries - possible?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By blurymind
:warning: Old Version Published before Godot 3 was released.

I need help parsing a json file into different dictionaries. The json file contains a a root with a number of {} structures, in which there is the data I want to parse to dictionaries.

So in essence I want to create a dictionary of startDialog1, node 2 and so on.

Alternatively they can be parsed to a single dictionary that contains all these subdictionaries.

[
{
	"title": "startDialog1",
	"tags": "",
	"body": "Starting here bla bla bla\nthis is a new line blah blah blah\n[[|node2]]",
	"position": {
		"x": 236,
		"y": 175
	},
	"colorID": 0
},
{
	"title": "node2",
	"tags": "",
	"body": "this is continuing from last time\nblah blah blah 2\nend",
	"position": {
		"x": 555,
		"y": 150
	},
	"colorID": 0
},
{
	"title": "dialogue2",
	"tags": "",
	"body": "blah blah dialogue 2 starts here\n[[|dialog2node2]]",
	"position": {
		"x": 238,
		"y": 424
	},
	"colorID": 6
},
{
	"title": "dialog2node2",
	"tags": "",
	"body": "dialogue2 continues here\n[[|node4]]",
	"position": {
		"x": 554,
		"y": 416
	},
	"colorID": 0
},
{
	"title": "Node4",
	"tags": "",
	"body": "end of dialogue 2",
	"position": {
		"x": 817,
		"y": 408
	},
	"colorID": 0
},
{
	"title": "Node5",
	"tags": "",
	"body": "Empty Text",
	"position": {
		"x": 788,
		"y": 143
	},
	"colorID": 0
}

]

How do I do that??
In this case I am trying to write a parser for YARN:

If I get it to work , I will share all the code on github.

:bust_in_silhouette: Reply From: Akien

I never tried it myself but I guess Dictionary.parse_json() would be a start?

It returns ERR_PARSE_ERROR for me. It’s a valid JSON, but Dictionary doesn’t seem to be able to parse a JSON that starts with an array:

[ { "example": 1 } ]

I opened an issue: #4232

neikeq | 2016-04-05 13:20

First I need to put this into an array. So I am doing a split of the string of the entire file :

yarnList = yarnFile.get_as_text().split('},{', true)
print (yarnList[0])

But it does not seem to work, this should only print:

{
	"title": "Start",
	"tags": "",
	"body": "make a choice please!\n\n[[choise 1|node2]]\n[[choise 2|node3]]",
	"position": {
		"x": 263,
		"y": 361
	},
	"colorID": 0
},

but it prints everything

blurymind | 2016-04-05 13:34

Coming from a REST API development background as my day job, it’s typically recommended to not use as the top level root, and instead either have a single object, or have an object containing a “things” array

{
  "items": [
    {"id": 1}, {"id": 2}
  ]
}

This should parse correctly with nearly any marshalling library (e.g. Jackson in Java).

For my work, this would be seen as best practice and also allows future modification of the object without breaking backward-compatibility. If you ever need to add things to your object, you would have to convert from a top-level array to an object. With the method above, you can easily add a new node/child/leaf without breaking BC

Brandon Lamb | 2016-10-22 13:48

@Brandon Lamb, can you please explain how to apply to this .json file in GDScript?
{
“items”: [
{“id”: 1}, {“id”: 2}
]
}

I mean how to write a code, which find out that we want to apply to “items”?
Thank you

Gleb | 2017-11-07 21:12

:bust_in_silhouette: Reply From: blurymind

Ok here is some progress!
This is actually a bug in the json parser in godot at the moment!

I got a nice workaround at the facebook group here

With some help I managed to solve this issue with a workaround.

var json = str('{"array":', yarnFile.get_as_text(), '}')
var dictionary = Dictionary()
dictionary.parse_json(json)
var yarn = dictionary["array"]

Calling dictionary[“array”] will return an Array containing the Dictionaries.

also as an extra I learned more about how to cut strings:

str.substr(0, str.length()-2)  is the same as what you would do in python with myString[:-2]
:bust_in_silhouette: Reply From: neikeq

This issue was fixed in 3.0 alpha. As of 3.0, any value can be placed as root of a json.
For more information: godotengine/godot#4232