json.get: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
(Fixed the example to show correct output and added some more explanation.) |
||
Line 49: | Line 49: | ||
e) 44 -- a value | e) 44 -- a value | ||
f) meow -- a value | f) meow -- a value | ||
g) [ | g) [44,"meow"] -- an array slice of 1 to 2 | ||
h) ["meow" | h) [1,44,"meow"] -- an array slice from 0 to end | ||
i) [ | i) ["meow",44,1] -- an array slice from 2 to 0 (reverse order) | ||
j) ["meow",44] -- an array slice from end.. | j) ["meow",44,1] -- an array slice from end to 0 (reverse order) | ||
ERROR a) -- java.lang.ArrayIndexOutOfBoundsException, can't use negatives with a single index param (works OK with slices). | |||
ERROR b) -- java.lang.ArrayIndexOutOfBoundsException, the last valid index is 2 | |||
Reverse Order is just if the 2nd number is lower than the first, except with -1 which means end of list. Think of it as a wrapping number line going from 0 to N. Starting at 0, then minus 1 brings you to end of list. -2 would be 2nd from end of list. But it only wraps once that way and not past the end of list. So, referencing N+1 will give you an out of bounds error like with ERROR b. | |||
|changes= | |changes= | ||
{{change|1.3b51|Added ability to return [[JSON Array]] slices.}} | {{change|1.3b51|Added ability to return [[JSON Array]] slices.}} |
Revision as of 11:22, 2 March 2020
json.get() Function
• Introduced in version 1.3b49
Returns the value in a JSON Array at the specified index, returns a slice of a JSON Array from the specified indexes, or returns the value from JSON Object for the specified key.
Usage
json.get(array, index)
json.get(array, start, end)
json.get(object, key, key, ...)
Parameters
array
- The JSON Array to retrieve the element from.index
- The numerical index of the element you want returned.start
- The starting index of the element you wish the slice to begin at.end
- The ending index of the element you wish the slice to end at.object
- The JSON Object to retrieve the element from.key
- The name of a field that should be returned. This parameter can exist more than once, if it does then a JSON Object is returned with all the specified elements.
When extracting slices: Negative numbers can be used as the offsets from the end of the array, -1
is the last element in the array, -2
is the second to last, and so on. If the end
index is smaller than the start
index then the array slice is returned in reverse. This does not work for single indices, so if you want to retrieve a single index, say the last one in an array, you do that like this: json.get(array, -1,-1). This way you take a slice of 1 index. To get the last element you thus need to do: json.get(json.get(array, -1,-1),0) as you get a slice and you want an element.
Examples
a) [h: jo = json.fromStrProp("a=1; b=44; cat=meow")] [r: json.get(jo, "b")] <br>
b) [h: jo = json.fromStrProp("a=1; b=44; cat=meow")] [r: json.get(jo, "XX")] <br>
c) [h: jo = json.fromStrProp("a=1; b=44; cat=meow")] [r: json.get(jo, "cat", "a")] <br>
d) [h: jo = json.fromStrProp("a=1; b=44; cat=meow")] [r: json.get(jo, "b", "XX")] <br>
e) [h: ja = json.fromList("1,44,meow")] [r: json.get(ja, 1)] <br>
f) [h: ja = json.fromList("1,44,meow")] [r: json.get(ja, 2)] <br>
g) [h: ja = json.fromList("1,44,meow")] [r: json.get(ja, 1, 2)] <br>
h) [h: ja = json.fromList("1,44,meow")] [r: json.get(ja, 0, -1)] <br>
i) [h: ja = json.fromList("1,44,meow")] [r: json.get(ja, 2, 0)] <br>
j) [h: ja = json.fromList("1,44,meow")] [r: json.get(ja, -1, 0)] <br>
ERROR a) [h: ja = json.fromList("1,44,meow")] [r: json.get(ja, -1)] <br>
ERROR b) [h: ja = json.fromList("1,44,meow")] [r: json.get(ja, 3)] <br>
Returns
a) 44 -- a value b) -- empty string "", as XX does not exist c) {"cat":"meow","a":1} -- a JSON object d) {"b":44,"XX":""} -- a JSON object e) 44 -- a value f) meow -- a value g) [44,"meow"] -- an array slice of 1 to 2 h) [1,44,"meow"] -- an array slice from 0 to end i) ["meow",44,1] -- an array slice from 2 to 0 (reverse order) j) ["meow",44,1] -- an array slice from end to 0 (reverse order) ERROR a) -- java.lang.ArrayIndexOutOfBoundsException, can't use negatives with a single index param (works OK with slices). ERROR b) -- java.lang.ArrayIndexOutOfBoundsException, the last valid index is 2Reverse Order is just if the 2nd number is lower than the first, except with -1 which means end of list. Think of it as a wrapping number line going from 0 to N. Starting at 0, then minus 1 brings you to end of list. -2 would be 2nd from end of list. But it only wraps once that way and not past the end of list. So, referencing N+1 will give you an out of bounds error like with ERROR b.
Version Changes
- 1.3b51 - Added ability to return JSON Array slices.
- 1.3b51 - Added ability to return JSON Objects of select fields from other JSON Objects.