# Casting data from backend to typed object on frontend

**URL:** <https://community.haxe.org/t/casting-data-from-backend-to-typed-object-on-frontend/3615>\
**Category:** Haxe\
**Tags:** haxe-js, haxe-php\
**Created:** [August 5, 2022, 10:38am UTC](https://community.haxe.org/t/casting-data-from-backend-to-typed-object-on-frontend/3615 "2022-08-05T10:38:47Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![MaxiHaxe](https://avatars.discourse-cdn.com/v2/letter/m/5fc32e/32.png) [@MaxiHaxe](https://community.haxe.org/u/MaxiHaxe)\
**Post date:** [August 5, 2022, 10:38am UTC](https://community.haxe.org/t/casting-data-from-backend-to-typed-object-on-frontend/3615/1 "2022-08-05T10:38:47Z")

</div>

Hello everyone,

I’am struggling with sending data from backend (native php) to typed structure on Frontend (js in haxe) in Haxe.

Let’s say that my backend sending data to js variable in html and main.js processing the rest…:

In var configuration.singleProductData I can reach all data from product (data showed in console from backend):

configuration.singleProductData: {  
id: 1,  
title: ‘New Product’  
prices: {  
EUR : {  
price: 10,  
formatted: 10,00 €  
},  
USD : {  
price: 11,  
price\_formatted: 11,00 $  
},  
GBP : {  
price: 9,  
price\_formatted: 9,00 Pounds  
},  
}  
}

In the haxe I have got prepared structures for product and prices:

typedef ProductItem = {  
public var id:Int;  
public var title:String;  
public var prices:Map\<String,Price\>;  
}

typedef Price = {  
public var price:Float;  
public var priceFormatted:String;  
}

```haxe

I am trying with many ways how to parse it from native php to structured form in Haxe but still failed:
1. I need to iterate each item in object because between variables on BE and FE is sometimes different naming
2. i need to specify Iterable or Iterator - i think its should have lots real examples
3. how to iterate `prices` - its a map not array and this is also struggeling how to get into ds.Map each price (eur, usd, ...)?

Do you have any showcase for processing of these data?

Thank you so much again,
```

---

<div class="post-metadata">

**Author:** ![MaxiHaxe](https://avatars.discourse-cdn.com/v2/letter/m/5fc32e/32.png) [@MaxiHaxe](https://community.haxe.org/u/MaxiHaxe)\
**Post date:** [August 5, 2022, 4:44pm UTC](https://community.haxe.org/t/casting-data-from-backend-to-typed-object-on-frontend/3615/2 "2022-08-05T16:44:35Z")

</div>

Trying also create iterator for map:

var iterator:haxe.iterators.MapKeyValueIterator = new MapKeyValueIterator(configuration.singleProductData.prices);

but got only compiler error:  
characters 19-53 : Not enough type parameters for haxe.iterators.MapKeyValueIterator

Thanks for any help!

---

<div class="post-metadata">

**Author:** ![Ilir-Liburn](https://avatars.discourse-cdn.com/v2/letter/i/82dd89/32.png) [@Ilir-Liburn](https://community.haxe.org/u/Ilir-Liburn)\
**Post date:** [August 5, 2022, 7:15pm UTC](https://community.haxe.org/t/casting-data-from-backend-to-typed-object-on-frontend/3615/3 "2022-08-05T19:15:05Z")

</div>

Hello,

configuration.singleProductData seems to contain json object, try haxelib search json (json2object seems interesting, get info via haxelib info json).

As error message explains: you are missing type parameters, e.g.

var iterator:haxe.iterators.MapKeyValueIterator\<String,Price\> = new MapKeyValueIterator(…);

or use type inference instead of explicit type hint

var iterator = new MapKeyValueIterator(…)

---

<div class="post-metadata">

**Author:** ![MaxiHaxe](https://avatars.discourse-cdn.com/v2/letter/m/5fc32e/32.png) [@MaxiHaxe](https://community.haxe.org/u/MaxiHaxe)\
**Post date:** [August 5, 2022, 8:14pm UTC](https://community.haxe.org/t/casting-data-from-backend-to-typed-object-on-frontend/3615/4 "2022-08-05T20:14:19Z")

</div>

Ohh, Thank you. I missed so tiny thing var iterator:haxe.iterators.MapKeyValueIterator\*\*\<String,Price\>\*\*

Now I got it around iterators… All my examples works fine.

Thank you so much!

---

<div class="post-metadata">

**Author:** ![MaxiHaxe](https://avatars.discourse-cdn.com/v2/letter/m/5fc32e/32.png) [@MaxiHaxe](https://community.haxe.org/u/MaxiHaxe)\
**Post date:** [August 5, 2022, 8:28pm UTC](https://community.haxe.org/t/casting-data-from-backend-to-typed-object-on-frontend/3615/5 "2022-08-05T20:28:33Z")

</div>

I also found solutions during iterating Object:

```haxe
for(n in Reflect.fields(single_product.prices)) {
    var priceData = Reflect.field(single_product.prices, n);
}

```

I would interest which of this solutions is more more efficient or faster and why?

---

<div class="post-metadata">

**Author:** ![Ilir-Liburn](https://avatars.discourse-cdn.com/v2/letter/i/82dd89/32.png) [@Ilir-Liburn](https://community.haxe.org/u/Ilir-Liburn)\
**Post date:** [August 5, 2022, 11:08pm UTC](https://community.haxe.org/t/casting-data-from-backend-to-typed-object-on-frontend/3615/6 "2022-08-05T23:08:07Z")

</div>

Reflection turns static into dynamic access on the static target, so it slow(est) solution. However, on dynamic targets like JS reflection should have smaller overload, however it is always better to check generated code.

Fastest solution could be for (key =\> val in map) because of the optimizations, but don’t take my words for granted because I haven’t checked generated code for long time.

---

<div class="post-metadata">

**Author:** ![back2dos](https://community.haxe.org/user_avatar/community.haxe.org/back2dos/32/30_2.png) [@back2dos](https://community.haxe.org/u/back2dos)\
**Post date:** [August 7, 2022, 7:04am UTC](https://community.haxe.org/t/casting-data-from-backend-to-typed-object-on-frontend/3615/7 "2022-08-07T07:04:58Z")

</div>

`Map` is not the same as an object with dynamic fields (in PHP terms, think of the former as `array` and the latter as `stdClass`).

Instead of `Map<String, Price>` you can use `haxe.DynamicAccess<Price>` and it will work as expected, with iterators and all.
