Get JSON type

Let’s say I have fetched this JSON from an API (server side):

{
id: "38eced94-df59-4e26-bb6e-c6e6938dd00e"
customer_reference: "Order 72"
amount_delivered: 0
amount_reserved: 27
amount_total: 29
}

And what I want to get from this JSON is the field type:

{
id: string
customer_reference: string
amount_delivered: integer
amount_reserved: integer
amount_total: integer
}

Is this somehow possible in Wappler on the server-side? I think the PHP function is gettype, but I don’t see anything similar in the visual tools in Wappler. Or is there a way how I can apply this PHP function server-side?

https://www.php.net/manual/en/function.gettype.php

Have a search here in the forum for custom formatters as there are a few posts on how to create. You could definitely do this by setting up your own.

1 Like

I could find that you can create a custom.php file in \dmxConnectLib\lib\formatters - but the code that I need to add there is quite abracadabra for me. Really don’t know how to make my own from scratch, until there is full tutorial.

create custom.php

<?php

namespace lib\core;

function formatter_gettype($val) {
  return gettype($val);
}

usage
{{xxxxx.gettype()}}

1 Like

Brilliant! That does put me in the right direction, only it analyses an entire object instead of each “line”:

image

The gettype is “object”, but I want the type per line, like this:

{
id: string
customer_reference: string
amount_delivered: integer
amount_reserved: integer
amount_total: integer
}

How can I do this using custom.php?

you can use repeater for returned results :slight_smile: so you dont need different custom php or custom function

I don’t think it’s possible repeat an object? Only arrays right?

Not sure how to use the repeater in this case.

Yes true . I mean, if it returns as an array and an object, you can use it this way.

I don’t understand what you mean. Can you point me in the right direction how to solve this?

Will the scheme returned from api always different ?

your schema ;

{
id: string
customer_reference: string
amount_delivered: integer
amount_reserved: integer
amount_total: integer
}

if no ;

set variables :

{{id.gettype()}}
{{customer_reference.gettype()}}
etc..

The schema from the API is always the same, unless the developers change something in their API.

create 5 variable
then use it new function inside it