Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to access inner inner array from json_parse in a loop #435

Open
SamuelMarks opened this issue Aug 4, 2024 · 1 comment
Open

Unable to access inner inner array from json_parse in a loop #435

SamuelMarks opened this issue Aug 4, 2024 · 1 comment
Assignees

Comments

@SamuelMarks
Copy link

SamuelMarks commented Aug 4, 2024

Describe The Bug

Was using the GitHub API to parse release objects within arrays within an object when I was unable to access elements at array index. Then realised I couldn't access it.

To Reproduce

From duck repl:

object = json_parse "{\"obj_array\": {\"obj\": [1, 2, 3]}}"
arr_rang = range 0 ${object.obj_array.obj.length}
#i = set 1
for i in ${arr_rang}
    a00 = get_by_name object.obj_array.obj[i]
    a01 = get_by_name object.obj_array.obj[${i}]
    a02 = get_by_name ${object.obj_array.obj[i]}
    a03 = get_by_name ${object.obj_array.obj[${i}]}
    a04 = set ${object.obj_array.obj[${i}]}
    a05 = set ${object.obj_array.obj[i]}

    obj_array0 = set ${object.obj_array}
    a06 = get_by_name obj_array0.obj[i]
    a07 = get_by_name obj_array0.obj[${i}]
    a08 = get_by_name ${obj_array0.obj[i]}
    a09 = get_by_name ${obj_array0.obj[${i}]}
    a10 = set ${obj_array0.obj[${i}]}
    a11 = set ${obj_array0.obj[i]}

    obj_array1 = get_by_name object.obj_array
    a12 = get_by_name obj_array1.obj[i]
    a13 = get_by_name obj_array1.obj[${i}]
    a14 = get_by_name ${obj_array1.obj[i]}
    a15 = get_by_name ${obj_array1.obj[${i}]}
    a16 = set ${obj_array1.obj[${i}]}
    a17 = set ${obj_array1.obj[i]}

    echo i=${i} a00=${a00}, a01=${a01}, a02=${a02}, a03=${a03}, a04=${a04}, a05=${a05}, a06=${a06}, a07=${a07}, a08=${a08}, a09=${a09}, a10=${a10}, a11=${a11}, a12=${a12}, a13=${a13}, a14=${a14}, a15=${a15}, a16=${a16}, a16=${a16}, a17=${a17}
end

However this works just fine:

v = get_by_name object.obj_array.obj[1]
echo v=${v}

Error Stack

i=0 a00=, a01=1, a02=, a03=, a04=]}, a05=, a06=, a07=, a08=, a09=, a10=]}, a11=, a12=, a13=, a14=, a15=, a16=]}, a16=]}, a17=
i=1 a00=, a01=2, a02=, a03=, a04=]}, a05=, a06=, a07=, a08=, a09=, a10=]}, a11=, a12=, a13=, a14=, a15=, a16=]}, a16=]}, a17=
i=2 a00=, a01=3, a02=, a03=, a04=]}, a05=, a06=, a07=, a08=, a09=, a10=]}, a11=, a12=, a13=, a14=, a15=, a16=]}, a16=]}, a17=
@SamuelMarks SamuelMarks changed the title Unable to access inner inner array from json_parse Unable to access inner inner array from json_parse in a loop Aug 4, 2024
@sagiegurari
Copy link
Owner

sagiegurari commented Aug 17, 2024

@SamuelMarks i can't see any issue

object = json_parse "{\"obj_array\": {\"obj\": [1, 2, 3]}}"
echo ${object.obj_array.length}

echo ${object.obj_array.obj.length}
count = set ${object.obj_array.obj.length}
echo count: ${count}
range = range 0 ${count}
for i in ${range}
    echo index: ${i}
    value = get_by_name object.obj_array.obj[${i}]
    echo value: ${value}
end

echo direct access: ${object.obj_array.obj[1]}

jsonstring = json_encode object
echo ${jsonstring}

outputs:

 duck ./test.ds
 
3 
count: 3 
index: 0 
value: 1 
index: 1 
value: 2 
index: 2 
value: 3 
direct access: 2 
{"obj_array":{"obj":["1","2","3"]}} 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants