Innula Zenovka Posted January 13, 2023 Share Posted January 13, 2023 According to https://jsonlint.com, this (which is an edited version of something I'm receiving from a remote server) is valid json { "playlists": [{ "id": "some key", "title": "Playlist 1", "description": null, "trackCount": 20 }, { "id": "another key", "title": "Playlist 2", "description": null, "trackCount": 30 }] } However, this returns JSON_INVALID, as does a similar test of the unescaped body I receive in the http_response event. string test = "{ \"playlists\": [{ \"id\": \"some key\", \"title\": \"Playlist 1\", \"description\": null, \"trackCount\": 20 }, { \"id\": \"another key\", \"title\": \"Playlist 2\", \"description\": null, \"trackCount\": 30 } ] }"; string json_type2string(string s) { if(s==JSON_OBJECT) { return "JSON_OBJECT"; }else if(s==JSON_ARRAY) { return "JSON_ARRAY"; }else if(s==JSON_NUMBER) { return "JSON_NUMBER"; }else if(s==JSON_STRING) { return "JSON_STRING"; }else if(s==JSON_NULL) { return "JSON_NULL"; }else if(s==JSON_TRUE) { return "JSON_TRUE"; }else if(s==JSON_FALSE) { return "JSON_FALSE"; }else if(s==JSON_DELETE) { return "JSON_DELETE"; } return "JSON_INVALID"; } default { state_entry() { llOwnerSay(json_type2string(test)); } } I must be missing something obvious, but what is it, please? Link to comment Share on other sites More sharing options...
Frionil Fang Posted January 13, 2023 Share Posted January 13, 2023 (edited) You're just comparing your entire JSON string to one of the JSON constants. Since it isn't any of them, it defaults to returning JSON_INVALID. What you presumably want to do is like llOwnerSay(json_type2string(llJsonValueType(test, []))); which will return the expected JSON_OBJECT; ValueType(test, ["playlists"]) gets you JSON_ARRAY, etc. Edited January 13, 2023 by Frionil Fang 1 Link to comment Share on other sites More sharing options...
Love Zhaoying Posted January 13, 2023 Share Posted January 13, 2023 1 minute ago, Frionil Fang said: You're just comparing your entire JSON string to one of the JSON constants. Since it isn't any of them, it defaults to returning JSON_INVALID. Beat me to it, and I jumped out of bed to check! default { state_entry() { llOwnerSay(json_type2string(test)); } } You really need to call llJsonValueType(): (from https://wiki.secondlife.com/wiki/Json_usage_in_LSL) string type = llJsonValueType( string json, list specifiers ); 1 Link to comment Share on other sites More sharing options...
Love Zhaoying Posted January 13, 2023 Share Posted January 13, 2023 If you did this: string sPlaylists; sPlaylists = llJsonGetValue(test, ["playlists"]); Then called llJsonValueType(sPlaylists) - it would return something you could pass to json_type2string(). Link to comment Share on other sites More sharing options...
Rolig Loon Posted January 13, 2023 Share Posted January 13, 2023 3 minutes ago, Frionil Fang said: You're just comparing your entire JSON string to one of the JSON constants. Since it isn't any of them, it defaults to returning JSON_INVALID. But test is a string. Are you saying that Innula should only be asking for the JSON type of, say, { \"id\": \"some key\", \"title\": \"Playlist 1\", \"description\": null, \"trackCount\": 20 } ? Link to comment Share on other sites More sharing options...
Love Zhaoying Posted January 13, 2023 Share Posted January 13, 2023 Just now, Rolig Loon said: But test is a string. Are you saying that Innula should only be asking for the JSON type of, say, { \"id\": \"some key\", \"title\": \"Playlist 1\", \"description\": null, \"trackCount\": 20 } ? She needs to call llJsonValueType()! https://wiki.secondlife.com/wiki/LlJsonValueType Link to comment Share on other sites More sharing options...
Love Zhaoying Posted January 13, 2023 Share Posted January 13, 2023 It looks to me like with your Json, if you called llJsonValueType() on the whole thing, you'd get JSON_OBJECT back. Link to comment Share on other sites More sharing options...
Rolig Loon Posted January 13, 2023 Share Posted January 13, 2023 Ah, OK. You can tell that I don't use JSON myself either. I'm not comfortable with the syntax yet. 1 Link to comment Share on other sites More sharing options...
Love Zhaoying Posted January 13, 2023 Share Posted January 13, 2023 I keep missing the syntax myself, it would be: llJsonValueType(test, [ ]) - for the whole thing llJsonValueType(test, ["playlists"]) - example for an element 1 Link to comment Share on other sites More sharing options...
Quistess Alpha Posted January 13, 2023 Share Posted January 13, 2023 (edited) 24 minutes ago, Innula Zenovka said: llOwnerSay(json_type2string(test)); to summarize, change that to llOwnerSay(json_type2string(llJsonValueType(test, []))); (ETA: as corrected by love below) Edited January 13, 2023 by Quistess Alpha 2 Link to comment Share on other sites More sharing options...
Love Zhaoying Posted January 13, 2023 Share Posted January 13, 2023 1 minute ago, Rolig Loon said: Ah, OK. You can tell that I don't use JSON myself either. I'm not comfortable with the syntax yet. Yeah, I use it every.single.day. with LSL but..I don't use the llJsonValueType() call much.. Sadly, a "string" is JSON_INVALID! So mostly I just use it to see am I looking at a JSON_OBJECT, JSON_ARRAY, or everything else is a "value". Link to comment Share on other sites More sharing options...
Love Zhaoying Posted January 13, 2023 Share Posted January 13, 2023 1 minute ago, Quistess Alpha said: 18 minutes ago, Innula Zenovka said: llOwnerSay(json_type2string(test)); to summarize, change that to llOwnerSay(json_type2string(llJsonValueType(test))); Except I got it wrong - it is: llOwnerSay(json_type2string(llJsonValueType(test, []))); 2 Link to comment Share on other sites More sharing options...
Love Zhaoying Posted January 13, 2023 Share Posted January 13, 2023 19 minutes ago, Innula Zenovka said: }else if(s==JSON_DELETE) { return "JSON_DELETE"; } You won't ever see this, or you shouldn't. When you try to call llJsonSetValue() with a value of JSON_DELETE..it..deletes. 1 Link to comment Share on other sites More sharing options...
Love Zhaoying Posted January 13, 2023 Share Posted January 13, 2023 it took me forever to start using it, but I suggest that when in question, you paste your Json into a Json Editor. I use "jsoneditoronline dot org", screenshot below of your Json pasted to one of the panes in it: Link to comment Share on other sites More sharing options...
Love Zhaoying Posted January 13, 2023 Share Posted January 13, 2023 45 minutes ago, Frionil Fang said: You're just comparing your entire JSON string to one of the JSON constants. Since it isn't any of them, it defaults to returning JSON_INVALID. What you presumably want to do is like llOwnerSay(json_type2string(llJsonValueType(test, []))); which will return the expected JSON_OBJECT; ValueType(test, ["playlists"]) gets you JSON_ARRAY, etc. Not sure if I missed it - but after your editing, your answer is pretty complete! Link to comment Share on other sites More sharing options...
Frionil Fang Posted January 13, 2023 Share Posted January 13, 2023 1 minute ago, Love Zhaoying said: Not sure if I missed it - but after your editing, your answer is pretty complete! I did the edit immediately after double checking I wasn't making a complete fool of myself before anyone else replied, so dunno! I took a further look out of curiosity and the JSON_??? constants are indeed valid characters in JSON itself, even if the constants themselves are not valid JSON as is. This means JsonGetValue would return JSON_INVALID as a response for a value that is valid and JSON_NULL for something that isn't null. Of course, JsonValueType for them is still JSON_STRING, so you would be able to tell if you cared enough. 1 Link to comment Share on other sites More sharing options...
Love Zhaoying Posted January 13, 2023 Share Posted January 13, 2023 3 minutes ago, Frionil Fang said: This means JsonGetValue would return JSON_INVALID as a response for a value that is valid and JSON_NULL for something that isn't null. Of course, JsonValueType for them is still JSON_STRING, so you would be able to tell if you cared enough. Yeah, I was really disappointed that llJsonValueType() returns JSON_INVALID for string values.. (hard to explain - at least it does for me). Link to comment Share on other sites More sharing options...
Frionil Fang Posted January 13, 2023 Share Posted January 13, 2023 2 minutes ago, Love Zhaoying said: Yeah, I was really disappointed that llJsonValueType() returns JSON_INVALID for string values.. (hard to explain - at least it does for me). Not sure what you mean exactly, but an arbitrary LSL string, i.e. "just some words", is not a valid Javascript object. A "\"string\"", i.e. a Javascript string inside the LSL string, is a valid Javascript object of type JSON_STRING without children. Link to comment Share on other sites More sharing options...
Love Zhaoying Posted January 13, 2023 Share Posted January 13, 2023 2 minutes ago, Frionil Fang said: Not sure what you mean exactly, but an arbitrary LSL string, i.e. "just some words", is not a valid Javascript object. A "\"string\"", i.e. a Javascript string inside the LSL string, is a valid Javascript object of type JSON_STRING without children. I believe that it happens when I iterate elements of a JSON_ARRAY. A normal "string" value returns JSON_INVALID for the type. (I'm pretty sure, can check that code later, I'm busy scripting now! lol) Link to comment Share on other sites More sharing options...
Love Zhaoying Posted January 13, 2023 Share Posted January 13, 2023 Yep.. sTemp = llJsonValueType(sParm, []); // Get Type ////////////// // JSON_ARRAY if (sTemp==JSON_ARRAY) { //Say("Array:"); // Call recursively if Array . } // if sType==JSON_ARRAY ////////////// // JSON_OBJECT - Handle Switch/Case else if (sTemp==JSON_OBJECT..) { . } // else sType==JSON_OBJECT - Handle Switch/Case ////////////// // Special case.. else { // Else Not JSON_ARRAY or JSON_OBJECT if (sTemp==JSON_INVALID...) { ..String are handled here! } } Link to comment Share on other sites More sharing options...
Frionil Fang Posted January 13, 2023 Share Posted January 13, 2023 Starting to get off topic, but I get JSON_STRING for a valid string's type, whether loose, as an object property or in an array. If it's malformed enough to return JSON_INVALID for the type, I also cannot retrieve the value. No idea without seeing the actual data in question. 1 Link to comment Share on other sites More sharing options...
Love Zhaoying Posted January 13, 2023 Share Posted January 13, 2023 8 hours ago, Frionil Fang said: Starting to get off topic, but I get JSON_STRING for a valid string's type, whether loose, as an object property or in an array. If it's malformed enough to return JSON_INVALID for the type, I also cannot retrieve the value. No idea without seeing the actual data in question. Perhaps I'll throw together a complete example next time it comes up. As you said, off topic. Link to comment Share on other sites More sharing options...
Innula Zenovka Posted January 13, 2023 Author Share Posted January 13, 2023 Thanks, everyone. I'm still not getting it, I'm afraid. In my example, string test = "{ \"playlists\": [{ \"id\": \"some key\", \"title\": \"Playlist 1\", \"description\": null, \"trackCount\": 20 }, { \"id\": \"another key\", \"title\": \"Playlist 2\", \"description\": null, \"trackCount\": 30 } ] }"; how do I find the value for first "id" key? I think it ought to be llJsonGetValue("test",["id",0]); but that doesn't work, and neither does anything else I try. Link to comment Share on other sites More sharing options...
Love Zhaoying Posted January 13, 2023 Share Posted January 13, 2023 (edited) 7 minutes ago, Innula Zenovka said: Thanks, everyone. I'm still not getting it, I'm afraid. In my example, string test = "{ \"playlists\": [{ \"id\": \"some key\", \"title\": \"Playlist 1\", \"description\": null, \"trackCount\": 20 }, { \"id\": \"another key\", \"title\": \"Playlist 2\", \"description\": null, \"trackCount\": 30 } ] }"; how do I find the value for first "id" key? I think it ought to be llJsonGetValue("test",["id",0]); but that doesn't work, and neither does anything else I try. "test" is the name of your variable. Try: llJsonGetValue(test, [..what to search for..]); "id" is not an array, so "0" won't find anything. What do you want to find? "Playlists" is an array, so llJsonGetValue(test, [playlists, 0, id]) would get the first playlist, and the value of "id". No matter what you are looking for, it appears "playlists" is your only top-level entry. So, no matter what you are looking for, it would have to be ["playlists",..]. Since you only have 2 "playlists" array entries, your choices are ["playlists",0..] and ["playlists",1..]. First level: "playlists": Key for KVP, of which the "value" is JSON_ARRAY. Next level: JSON_ARRAY, so you must provide an "index" to select an entry in the JSON_ARRAY Next level: JSON_OBJECT. so provide a "key".. Edited January 13, 2023 by Love Zhaoying 1 Link to comment Share on other sites More sharing options...
Frionil Fang Posted January 13, 2023 Share Posted January 13, 2023 (edited) 9 minutes ago, Innula Zenovka said: how do I find the value for first "id" key? LlJsonGetValue(test, ["playlists", 0, "id"]); It's an array called playlists, with the first member being 0. If it's in curly braces, it's an object and you seek values by the label:value pairs. If it's square brackets it's qn array and you seek by 0-based indices. Objects can contain arrays and vice versa. Edited January 13, 2023 by Frionil Fang 2 1 Link to comment Share on other sites More sharing options...
Recommended Posts
Please take a moment to consider if this thread is worth bumping.
Please sign in to comment
You will be able to leave a comment after signing in
Sign In Now