I really don't get how to use JSON

Not favoritedFavorited Favorited 2 favourites
  • 6 posts
From the Asset Store
Piano Tiles with Sound Keys - I don't have 50 words to type here
  • I've seen the official examples, and they are all about reading or simple writing of JSON. I really can't figure out how to create a multi-layered structure. I just want to ask you guys how to build the following structure step by step?

    For example, after I created an array for the of a book, I can't add additional keys anymore. I don't understand how to proceed. I don't know how to create an array within another array. Thank you!

    {
    	"books": [
    		{
    			"title": "Everyday Italian",
    			"authors": ["Giada De Laurentiis"],
    			"year": 2005,
    			"price": "30.00"
    		},
    		
    		{
    			"title": "Harry Potter",
    			"authors": ["J K. Rowling"],
    			"year": 2005,
    			"price": "29.99"
    		},
    		
    		{
    			"title": "XQuery Kick Start",
    			"authors": [
    				"James McGovern",
    				"Per Bothner",
    				"Kurt Cagle",
    				"James Linn",
    				"Vaidyanathan Nagarajan"
    			],
    			"year": 2003,
    			"price": "49.99"
    		},
    		
    		{
    			"title": "Learning XML",
    			"authors": ["Erik T. Ray"],
    			"year": 2003,
    			"price": "39.95"
    		}
    	]
    }

    Tagged:

  • Step1: Add a Array Object

    {
     "books": []
    }
    {"is-c3-clipboard-data":true,"type":"events","items":[{"eventType":"block","conditions":[{"id":"on-start-of-layout","objectClass":"System"}],"actions":[{"id":"set-array","objectClass":"JSON","parameters":{"path":"\"books\"","size":"0"}}]}]}

    -----------------------

    Step2: Add a dictionary Object in the Array

    {
    	"books": [
    		{}
    	]
    }
    
    {"is-c3-clipboard-data":true,"type":"events","items":[{"eventType":"block","conditions":[{"id":"on-start-of-layout","objectClass":"System"}],"actions":[{"id":"set-array","objectClass":"JSON","parameters":{"path":"\"books\"","size":"0"}},{"id":"push-value","objectClass":"JSON","parameters":{"where":"back","path":"\"books\"","value":"0"}},{"type":"comment","text":"add an object/dictionary: {}"},{"id":"set-object","objectClass":"JSON","parameters":{"path":"\"books.\" & 0"}}]}]}

    Here we need to explain,

    The simplest explanation is that you can think of JSON as an enhanced version of Dictionary and Array, which can use the superset object of dictionary {"key": value} and array [0,1,2..] at the same time.

    But when you need to use them, you need to declare whether the path is a dictionary or an array. Therefore, you need to use Set Array and Set Object action to change their types.

    -----------------------

    Step3: Add a Key in the dictionary

    {
    	"books": [
    		{
    			"title": "Everyday Italian"
    		}
    	]
    }
    {"is-c3-clipboard-data":true,"type":"events","items":[{"eventType":"block","conditions":[{"id":"on-start-of-layout","objectClass":"System"}],"actions":[{"id":"set-array","objectClass":"JSON","parameters":{"path":"\"books\"","size":"0"}},{"type":"comment","text":"add a book"},{"id":"push-value","objectClass":"JSON","parameters":{"where":"back","path":"\"books\"","value":"0"}},{"id":"set-object","objectClass":"JSON","parameters":{"path":"\"books.\" & 0"}},{"type":"comment","text":"[b]Set Path[/b] can make the JSON object to enter this path, so that you can use \".title\" to reference the [u]relative path[/u].\n\nIf you don't do this, you need to use an [u]absolute path[/u]: \"books.0.title\""},{"id":"set-path","objectClass":"JSON","parameters":{"path":"\"books.\" & loopindex"}},{"id":"set-value","objectClass":"JSON","parameters":{"path":"\".title\"","value":"\"Everyday Italian\""}}]}]}

    -----------------------

    Step4: Add a More Key

    {
    	"books": [
    		{
    			"title": "Everyday Italian",
    			"authors": ["Giada De Laurentiis"],
    			"year": 2005,
    			"price": "30.00"
    		}
    	]
    }
    

    Note that because authors is an array, you need to use Set Array to change its type.

    {"is-c3-clipboard-data":true,"type":"events","items":[{"eventType":"block","conditions":[{"id":"on-start-of-layout","objectClass":"System"}],"actions":[{"id":"set-array","objectClass":"JSON","parameters":{"path":"\"books\"","size":"0"}},{"type":"comment","text":"add a book"},{"id":"push-value","objectClass":"JSON","parameters":{"where":"back","path":"\"books\"","value":"0"}},{"id":"set-object","objectClass":"JSON","parameters":{"path":"\"books.\" & 0"}},{"id":"set-path","objectClass":"JSON","parameters":{"path":"\"books.\" & loopindex"}},{"type":"comment","text":"info"},{"id":"set-value","objectClass":"JSON","parameters":{"path":"\".title\"","value":"\"Everyday Italian\""}},{"id":"set-array","objectClass":"JSON","parameters":{"path":"\".authors\"","size":"0"}},{"id":"push-value","objectClass":"JSON","parameters":{"where":"back","path":"\".authors\"","value":"\"Giada De Laurentiis\""}},{"id":"set-value","objectClass":"JSON","parameters":{"path":"\".year\"","value":"2005"}},{"id":"set-value","objectClass":"JSON","parameters":{"path":"\".price\"","value":"\"30.00\""}}]}]}

    -----------------------

    Step5: Using Array Object Data

    {"is-c3-clipboard-data":true,"type":"events","items":[{"eventType":"block","conditions":[{"id":"on-start-of-layout","objectClass":"System"}],"actions":[{"id":"request-project-file","objectClass":"AJAX","parameters":{"tag":"\"\"","file":"books.json"}},{"id":"wait-for-previous-actions","objectClass":"System"},{"id":"load","objectClass":"Array","parameters":{"json":"AJAX.LastData"}},{"id":"signal","objectClass":"System","parameters":{"tag":"\"json\""}}]},{"eventType":"block","conditions":[{"id":"on-signal","objectClass":"System","parameters":{"tag":"\"json\""}}],"actions":[{"id":"set-array","objectClass":"JSON","parameters":{"path":"\"books\"","size":"0"}}],"children":[{"eventType":"block","conditions":[{"id":"for","objectClass":"System","parameters":{"name":"\"book\"","start-index":"0","end-index":"Array.Height-1"}}],"actions":[{"type":"comment","text":"add a book"},{"id":"push-value","objectClass":"JSON","parameters":{"where":"back","path":"\"books\"","value":"0"}},{"id":"set-object","objectClass":"JSON","parameters":{"path":"\"books.\" & loopindex"}},{"id":"set-path","objectClass":"JSON","parameters":{"path":"\"books.\" & loopindex"}}],"children":[{"eventType":"block","conditions":[{"id":"for","objectClass":"System","parameters":{"name":"\"info\"","start-index":"0","end-index":"Array.Width-1"}}],"actions":[{"type":"comment","text":"info"},{"id":"set-value","objectClass":"JSON","parameters":{"path":"\".title\"","value":"Array.At(0, loopindex(\"book\"))"}},{"id":"set-array","objectClass":"JSON","parameters":{"path":"\".authors\"","size":"0"}},{"id":"split-string","objectClass":"bookAuthors","parameters":{"string":"Array.At(1, loopindex(\"book\"))","separator":"\",\"","type":"string"}},{"id":"set-value","objectClass":"JSON","parameters":{"path":"\".year\"","value":"Array.At(2, loopindex(\"book\"))"}},{"id":"set-value","objectClass":"JSON","parameters":{"path":"\".price\"","value":"Array.At(3, loopindex(\"book\"))"}}],"children":[{"eventType":"block","conditions":[{"id":"for-each-element","objectClass":"bookAuthors","parameters":{"axes":"x"}}],"actions":[{"id":"push-value","objectClass":"JSON","parameters":{"where":"back","path":"\".authors\"","value":"trim(bookAuthors.CurValue)"}}]}]}]}]}]}

    -----------------------

    Finally, share a little tip.

    When I use JSON, I usually put a Text Input object in the scene and set its Type property to Textarea. Then set Text to JSON.ToBeautifiedString. This way you can easily inspect the contents of the JSON object

    {"is-c3-clipboard-data":true,"type":"events","items":[{"eventType":"block","conditions":[{"id":"every-tick","objectClass":"System"}],"actions":[{"id":"set-text","objectClass":"TextInput","parameters":{"text":"JSON.ToBeautifiedString"}}]}]}
    
  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • OHMAN, this should on the official example at all!

    really thanks a lot, now i get it finally

  • Just to add some additional information — in that example, the JSON contains an array of books: "books":[...] This approach works well when you have many books. However, it's not necessary to store data in arrays when using JSON. In fact, I rarely do that.

    For example, if you want to store stats for different weapons in an RPG game, using an array means you'll have to iterate through it every time you need to find a specific weapon:

    {
     "weapons": [
     {
     "name": "Short Sword",
     "type": "melee",
     "damage": 8,
     "range": 1,
     "weight": 3
     },
     {
     "name": "Longbow",
     "type": "ranged",
     "damage": 12,
     "range": 10,
     "weight": 4
     },
     {
     "name": "Fire Staff",
     "type": "magic",
     "damage": 15,
     "range": 6,
     "weight": 5
     }
     ]
    }
    

    To find a weapon you will need to loop thought the array:

    JSON For Each element in "weapons"
    JSON compare ".name"="Short Sword"
    

    It's much easier to store each weapon as a separate object instead:

    {
     "short_sword": {
     "type": "melee",
     "damage": 8,
     "range": 1,
     "weight": 3
     },
     "longbow": {
     "type": "ranged",
     "damage": 12,
     "range": 10,
     "weight": 4
     },
     "fire_staff": {
     "type": "magic",
     "damage": 15,
     "range": 6,
     "weight": 5
     }
    }
    

    Then you can access any weapon and its stats directly with a single expression, for example:

    JSON.get("fire_staff.damage")
    

    And of course objects can contain nested objects and arrays:

  • Thanks guys!

    You've opened my eyes, for some reason I've always used arrays or dictionaries and sometimes put lists in them.

    I've always used JSON just to load or store arrays, but it turns out to be so handy!

  • Thanks XHXIAIEIN and dop2000, I'm saving both of your responses in my personal C3 cheatsheets. Really made a few things clear that I didn't get from the manual or sample projects.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)