How do I get the current key of an object in a json file?

0 favourites
  • 12 posts
From the Asset Store
Random Maze Generator with Door & Key System - tutorial capx
  • I am struggling to fetch the key of an object in a json-array:

    [{"foo": "bar"}]

    How can I read the "foo" key? I don't need the value of "foo"; I only need the key "foo" itself.

    I am iterating over all items and JSON.CurrentKey only returns the array index, unfortunately.

    The documentation says > CurrentKey

    In a For each loop, a string of the current key name. If an array is being looped, the current key is a string of the current index, e.g. "0", "1"...

    but it does not say how to get the actual key name when looping over an array. Any ideas?

  • Current key is the name, current value is the value of that key.

    Provided you have the correct path.

    If its not doing that then make a test project to someone to debug.

  • Current key is the name, current value is the value of that key.

    Provided you have the correct path.

    If its not doing that then make a test project to someone to debug.

    Current Key is index of an array as the documentation says. But maybe I am missing something. here is the example project:

    easyupload.io/tws6mx

  • Yeah its not working as expected.

    I think there's quite a bit missing in the plug actually, but since it's so hard to use, people don't use it, and report issues.

  • I took a look at the example project, and the current key is working like an index.

    So it's either not meant to return the name, or its bugged, and it doesn't like something about your json.

  • Was the format [{"foo": "bar"}] readable?

    I try using ({"foo": "bar"}) seems okay.

  • Was the format [{"foo": "bar"}] readable?

    I try using ({"foo": "bar"}) seems okay.

    ({"foo": "bar"}) is not valid JSON syntax.

  • {"foo": "bar"}

    Json-for each entry in "items", System-Set Key to Json.CurrentKey

    Returns foo.

    You could also use this format.

    {"foo": [{"bar":"bar1"}]}

  • Fireche

    You are running into a subtlety.

    CurrentKey

    In a For each loop, a string of the current key name. If an array is being looped, the current key is a string of the current index, e.g. "0", "1"...

    Since your data is in an array, CurrentKey will have indexes to the array. What you need to do then is load what is at that path in the json into another JSON plugin instance and then loop through that. Because the new loop in on a json object, CurrentKey will have the key names as strings.

    dropbox.com/s/3zzdt1yjk9riwwr/ReadingJSON.c3p

    The example does just that. The only thing which is rather cryptic is the path you need to give to GetAsCompactString to extract a single item from the json array.

    For this example JSON, doing JSON.GetAsCompactString("array.0") gets you the first item as a string, which you can load into another JSON object using the Parse action.

    	{
    		"array": [
    			{"foo": "1"},
    			{"bar": "2"}
    		]
    	}
    

    An extra detail I noticed while puting the example together, it seems the path you need to give to GetAsCompactString has to be absolute.

  • Fireche

    You are running into a subtlety.

    > CurrentKey

    > In a For each loop, a string of the current key name. If an array is being looped, the current key is a string of the current index, e.g. "0", "1"...

    Since your data is in an array, CurrentKey will have indexes to the array. What you need to do then is load what is at that path in the json into another JSON plugin instance and then loop through that. Because the new loop in on a json object, CurrentKey will have the key names as strings.

    https://www.dropbox.com/s/3zzdt1yjk9riwwr/ReadingJSON.c3p?dl=0

    The example does just that. The only thing which is rather cryptic is the path you need to give to GetAsCompactString to extract a single item from the json array.

    For this example JSON, doing JSON.GetAsCompactString("array.0") gets you the first item as a string, which you can load into another JSON object using the Parse action.

    > 	{
    		"array": [
    > 			{"foo": "1"},
    > 			{"bar": "2"}
    > 		]
    	}
    

    An extra detail I noticed while puting the example together, it seems the path you need to give to GetAsCompactString has to be absolute.

    It's a little weird since dictionaries do return the key name, and this is essentially the same thing.

    Also looking at the example browser projects json example the for each

    For each entry in "books", text append "Book title: " & JSON.Get(".title") & newline

    Is a little convoluted since the "books" string in automatically implied rather than also returning the index.

    Then don't get me started on how that works with the dot in the json.get. I mean ok sure its key.pair, but using .pair really throws me a brain fart.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Fireche

    You are running into a subtlety.

    > CurrentKey

    > In a For each loop, a string of the current key name. If an array is being looped, the current key is a string of the current index, e.g. "0", "1"...

    Since your data is in an array, CurrentKey will have indexes to the array. What you need to do then is load what is at that path in the json into another JSON plugin instance and then loop through that. Because the new loop in on a json object, CurrentKey will have the key names as strings.

    https://www.dropbox.com/s/3zzdt1yjk9riwwr/ReadingJSON.c3p?dl=0

    The example does just that. The only thing which is rather cryptic is the path you need to give to GetAsCompactString to extract a single item from the json array.

    For this example JSON, doing JSON.GetAsCompactString("array.0") gets you the first item as a string, which you can load into another JSON object using the Parse action.

    > 	{
    		"array": [
    > 			{"foo": "1"},
    > 			{"bar": "2"}
    > 		]
    	}
    

    An extra detail I noticed while puting the example together, it seems the path you need to give to GetAsCompactString has to be absolute.

    thank you, this works fine :) maybe the JSON plugin could be updated to support this without the use of having to instantiate a temp object? :P

  • I think in this case the solution was a little bit strange because the problem was not what you usually use JSON for, which is getting the values for the keys you know about.

    The plugin is setup to easily access values rather than keys.

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