Rand commented on a Page, JSON for MIRC  -  Jun 25, 2015

I'm having a bit of a problem. Lets say you have a JSON string like such:
{ "200":[["one","two","three"],[1,2,3]]}

Say "200" is an ID. To get the value "one" you'd need to do this: $json(test, 200,0,1)

But what if you don't KNOW that the ID "200" is going to be there? Is there a way to iterate through the objects without knowing the object name with this script? I'm able to do this in C#, but I can't for the life of me figure out how to do this through this script. (Also, do you intend on adding a ".count" suffix, like Timi's?)

SReject  -  Jun 25, 2015

You can use $json().fuzzy and $JSON().fuzzyPath to access data as though its indexed. Using your example, you would do $JSON(test, 0, 0, 1).fuzzy to get the value.

As far as .count, there's already a property for it: $JSON().length

Rand  -  Jun 25, 2015

Okay, that sort of works, but I've run into a little bit of a problem, rather than giving a winded explanation, I'll give you a small bit of sample code:

alias fuztest {
jsonopen -d test {"1":["one"],"3":["three"],"2":["two"]}
echo -a > $json(test).error
echo -a Result: $json(test, 0, 0).fuzzypath
echo -a Result: $json(test, 1, 0).fuzzypath
echo -a Result: $json(test, 2, 0).fuzzypath
}
The result is this:

Result: ["1"]["0"]
Result: ["1"]["0"]
Result: ["2"]["0"]

Now, as you can see it skips over "3" entirely. It'd be index'ed in the second spot, but .fuzzy lives up to the name fuzzy and checks to see if there's a key named that first?

Is there any way we can get it to only check for it by index? and is there anyway to see the count of the base? like {id:[],id:[]} would be 2. I know .length works as a .count, but just doing a $json(test).length doesn't work. :x

SReject  -  Jun 25, 2015

Mkay, i understand the issue as far as fuzzy 'sort of' working, and will see what I can do to fix it. Currently the script checks to see if the specified entry is a key and if it exists, returns it as such. If the key doesn't exist it loops over the object looking for the nTH item. So objects with numerical keys interfere when trying to access the object by indexes. For this I will most likely have to add another suffix that assumes all keys not wrapped in ""s are to be index. ETA: within a week

As far as .count vs .length, currently my .length works for arrays and strings. Are you suggesting it also work for objects? Or am I wrong, in what you are suggesting. If so, what would be the expected behavior of $json(test).length assuming the input data is from your example above.

Rand  -  Jun 26, 2015

In my example, i would expect It to return 3. It would count the keys. so in {"test":["abc",123]} would return 1 and {"test":["abc",123], "yeah":"exactly"} would return 2. so i guess Yeah, for objects if possible.

looking forward to the update. good work.

Sign in to comment

Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.