How do I loop through JSON Array

I’m having difficulties at the moment formatting this JSON array into something
that i can iterate over with a foreach loop.

I am using the JSON utility plugin. I don’t have a code for trying to format the JSON array.
What I have tried is to index the field that has its’ value as the JSON array, when converting to a string and printing, it only shows me one of the elements as oppose to all of them.

{"LaserData": "{"laser":[ {"x": 0.5, "y": 0.5}, ... {"x": 0.5, "y": 0.5} ]}" }
This is exactly how my JSON table looks from the start, The value for LaserData is a string, but it is converted to a JSON. From here I’m printing the value that i get when i Index the ‘laser’ field. It prints {"x": 0.5, "y":0.5}. I’m unsure if i’m getting the full array.

This is what I have momentarily, to recapilutate I want to be able to iterate over the “laser” array. I’m working with UE5 (V5.0.3).

Thanks in advance for anyone’s time and help.

Hey @GIS_UE5, thanks for posting on the forums, welcome!

I think you’d have the best success creating a for loop that iterates over the contents of the JSON, and appends them to an array you’ve created in Blueprints.
I think the only error your’re currently getting is because you’re doing the same operations twice, and I believe that changes the type of object you’re referring to in the Blueprint.

Happy coding, I hope this helps!
-Zen

1 Like

Hey Zen, thanks for the quick reply.

I was thinking the same thing, the only issue is how would I index each
element? To get the field it requires a string key, the object is a JSON array
so it doesn’t have keys.

Is there a way I can get the nTH value in a JSON array using the JSON utilities plugin?

EDIT: An observed issue as well, when printing the ‘laser’ field, it returns only the first value in that array, Get Field (laser):laserArray > Load From String (laserArray):StringOut > Print String (StringOut) Prints laser[0] : {“x”: 0.5, “y”: 0.5}

Regards
Alex

@GIS_UE5,
I’m not entirely sure about a way to keep it within the JSON, just because you’d have to search within JSON’s keys as you said. In Unreal’s JSON Utilities package there is a JSON to UStruct function, which you could then iterate through as a normal array. Here’s a link to some docs to help you get started: FJsonObjectConverter | Unreal Engine Documentation
I hope this can help, and happy coding!
-Zen

2 Likes

Thank you Zen,
I haven’t seen that up until now, I’ll explore it and I’ll mark your
message as a solution after for anyone else facing the same problem.

Regards
Alex

2 Likes

Hello Zen,
Just a quick update, I’ve been trying to look into using the function in the header file
and somehow creating a function block that I can use in blueprint, Would you have
any resources on that?

Kind Regards
Alex

If you expect the elements of an array to be objects, you can pass an array of objects to the wildcard.

Log:
image

My Products

5 Likes

Hello Supremative,
I’ve already tried that, I’m getting a compile error
I need to specify a specific type for the foreach loop block.


How would I Rectify this issue?

Kind Regards
Alex

1 Like

Connect from right to left

10 Likes

That worked out,
Many thanks for the assistance.

Kind Regards
Alex

you are a hero. i would never have thought to connect in reverse order.

how can we update documentation on this!?

very cool! it is worked for me