ID:2756308
 
(See the best response by Kaiochao.)
Problem description:
Hallo i wondering how can i convert JSON file into structure like this one.. (based on lists)::pika:

var/list/Dialogue = list(
"MrSatan" = list(
"INTRO_1" = list(// shown dialogue option always
"Options" = list(list("Nothing" = "End"), // shown dialogue option always
list("Over Level" = 9000, "I'm Over 9000 Lv" = "STRONGER_THAN_ME"),// shown dialogue option when player is over 9000 lv
list("Under Level" = 9000, "Teach me how to fight" = "NEW_STUDENT")// shown dialogue option when player is under 9000 lv
),
"Body" = list("Hi i'm the strongest man in the world!","What are you looking for?")// dialogue body
),
"STRONGER_THAN_ME" = list(
"Options" = list(list("Let's begin!" = "End")),
"Body" = list("Are you redy?")

),
"NEW_STUDENT" = list(
"Options" = list(list("Sure" = "End"), list("No way!" = "End")),
"Body" = list("Bring me 5 flowers..")
),
)
)

//JSON


[
{
"MrSatan": {
"INTRO_1":
{
"Options" : {"Let's begin!" : "End"},
"Body" : ["Are you redy from JSON"]
}
}
}
]




This is how i've made it with datum. It's work fine. Its much easier for me:(


card
parent_type = /obj
var/list/someVar

New(list/data)
name = data["name"]
icon = 'Cards.dmi'
icon_state=data["icon_state"]
someVar = data["someVar"]

var/list/global_cards
proc/LoadCards(fname)
var/i
var/card/C
global_cards = json_decode(file2text(fname))
for(i in 1 to global_cards.len)
C = new/card(global_cards[i])
global_cards[i] = C.name
global_cards[C.name] = C

mob/verb/OnInit()
set category="json";
world << "starting json load";
LoadCards("Code/data.json")


proc/GetCard(cname)
var/card/c = global_cards[cname]
return c


//JSON

[
{
"name": "As",
"icon_state": "as",
"someVar": ["some1", "some2"]
},
{
"name": "Król",
"icon_state": "k",
"someVar": ["some1", ["some2_1+","some2_2"]]
},
{
"name": "10",
"icon_state": "10",
"someVar": "Dyszka"
},
{
"name": "9",
"icon_state": "10",
"someVar": "Dziewi&#281;&#263;"
}
]




I am not really completely sure what you are asking. But I do feel as if the "json_decode" and "json_encode" are what you are looking for. There also more then a few libraries on this matter as well.
In response to Shadowkaroth
You're right. I try once again. In my dialogues system i use dialogue tree looking like this:
var/list/Dialogue = list(
"MrSatan" = list(
"INTRO_1" = list(// shown dialogue option always
"Options" = list(list("Nothing" = "End"), // shown dialogue option always
list("Over Level" = 9000, "I'm Over 9000 Lv" = "STRONGER_THAN_ME"),// shown dialogue option when player is over 9000 lv
list("Under Level" = 9000, "Teach me how to fight" = "NEW_STUDENT")// shown dialogue option when player is under 9000 lv
),
"Body" = list("Hi i'm the strongest man in the world!","What are you looking for?")// dialogue body
),
"STRONGER_THAN_ME" = list(
"Options" = list(list("Let's begin!" = "End")),
"Body" = list("Are you redy?")

),
"NEW_STUDENT" = list(
"Options" = list(list("Sure" = "End"), list("No way!" = "End")),
"Body" = list("Bring me 5 flowers..")
),
)
)


I wanna import JSON file and convert this JSON to structure like above one.

My main problem is I'm not sure how dm convert JSON file.

I think ["a":"a"] convert to list("a"="a") but what about {dictionaries?}
How will look JSON after convert to dm

[
{
"MrSatan": {
"INTRO_1":
{
"Options" : {"Let's begin!" : "End"},
"Body" : ["Are you redy from JSON"]
}
}
}
]

Best response
If you have a .json file on disk, to pass its contents to json_decode(), first you need to read the contents of the file with file2text().
// file can also be provided at runtime via input() as file
var/file = 'some file.json'
var/file_contents = file2text(file)
var/list/json_decoded = json_decode(file_contents)

// in short, json_decode(file2text(file))


I think ["a":"a"] convert to list("a"="a") but what about {dictionaries?}
["a":"a"] is not valid JSON. {"a":"a"} is already dictionary which will convert to list("a"="a"). [] is for arrays with no associations.
In response to Kaiochao
Thanks, i know where i made mistake, and it doesn't work properly. I made 2 mistakes.
1'st was i forget after convert my object "MrSatan" was in list.
My 2nd problem was, i forget about the [] in "Options".

[//<<-- don't need it
{
"MrSatan": {
"INTRO_1":
{
"Options" : [//<<-- this one
{"Let's begin!" : "End"}
],//<<--
"Body" : ["Are you redy from JSON"]
}
}
}
]//<<--