This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
0 votes

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:
https://github.com/InfiniteAmmoInc/Yarn
If I get it to work , I will share all the code on github.

in Engine by (100 points)

3 Answers

+1 vote
Best answer

Ok here is some progress!
This is actually a bug in the json parser in godot at the moment!
https://github.com/godotengine/godot/issues/4232

I got a nice workaround at the facebook group here
https://www.facebook.com/groups/godotengine/permalink/756821351121128/

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]
by (100 points)
0 votes

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

by (1,957 points)

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

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

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, 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

0 votes

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

by (488 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.