How can I merge 2 or more json results

hello @patrick,

Can you guide me please I need to merge 2 or more json data .
So I have 2 repeater in 1 server action and I need to merge them in 1 json . how can I do this ?

@george @Teodor do you have an idea ? It is important for me

Just a idea, merge to database then output to JSON which is of course the default server action format.
Never tried that but it’s an idea

thank you @Hyperbytes ,
but issue is different . I want to merge 2 server connect repeater
first repeater is returning all table headers and second repeater returning rows.
and rows returning without keys.

Could you provide a small sample of the data and how the result should look like. Also tell me if you use PHP, ASP or .NET.

Hello @patrick

I am working on iyzico.com (payment gateway) and you can see the all details on this link https://dev.iyzipay.com/en/checkout-form/initialize#response

I’m using PHP
API EndPoint : https://sandbox-api.iyzipay.com/
example json (All of json)

//Request
 {
    "locale": "tr",
    "conversationId": "123456789",
    "price": "1.0",
    "paidPrice": "1.2",
    "installment": 1,
    "paymentChannel": "WEB",
    "basketId": "B67832",
    "paymentGroup": "PRODUCT",
    "paymentCard": {
        "cardHolderName": "John Doe",
        "cardNumber": "5528790000000008",
        "expireYear": "2030",
        "expireMonth": "12",
        "cvc": "123"
    },
    "buyer": {
        "id": "BY789",
        "name": "John",
        "surname": "Doe",
        "identityNumber": "74300864791",
        "email": "email@email.com",
        "gsmNumber": "+905350000000",
        "registrationDate": "2013-04-21 15:12:09",
        "lastLoginDate": "2015-10-05 12:43:35",
        "registrationAddress": "Nidakule Göztepe, Merdivenköy Mah. Bora Sok. No:1",
        "city": "Istanbul",
        "country": "Turkey",
        "zipCode": "34732",
        "ip": "85.34.78.112"
    },
    "shippingAddress": {
        "address": "Nidakule Göztepe, Merdivenköy Mah. Bora Sok. No:1",
        "zipCode": "34742",
        "contactName": "Jane Doe",
        "city": "Istanbul",
        "country": "Turkey"
    },
    "billingAddress": {
        "address": "Nidakule Göztepe, Merdivenköy Mah. Bora Sok. No:1",
        "zipCode": "34742",
        "contactName": "Jane Doe",
        "city": "Istanbul",
        "country": "Turkey"
    },
    "basketItems": [
        {
            "id": "BI101",
            "price": "0.3",
            "name": "Binocular",
            "category1": "Collectibles",
            "category2": "Accessories",
            "itemType": "PHYSICAL"
        },
        {
            "id": "BI102",
            "price": "0.5",
            "name": "Game code",
            "category1": "Game",
            "category2": "Online Game Items",
            "itemType": "VIRTUAL"
        },
        {
            "id": "BI103",
            "price": "0.2",
            "name": "Usb",
            "category1": "Electronics",
            "category2": "Usb / Cable",
            "itemType": "PHYSICAL"
        }
    ],
    "currency": "TRY",
    "callbackUrl": "https://www.merchant.com/callback"
}

Problem is
first part of json coming from payment form.

"paymentCard": {
        "cardHolderName": "John Doe",
        "cardNumber": "5528790000000008",
        "expireYear": "2030",
        "expireMonth": "12",
        "cvc": "123"
    },

second part of json coming from server connect query

"buyer": {
        "id": "BY789",
        "name": "John",
        "surname": "Doe",
        "identityNumber": "74300864791",
        "email": "email@email.com",
        "gsmNumber": "+905350000000",
        "registrationDate": "2013-04-21 15:12:09",
        "lastLoginDate": "2015-10-05 12:43:35",
        "registrationAddress": "Nidakule Göztepe, Merdivenköy Mah. Bora Sok. No:1",
        "city": "Istanbul",
        "country": "Turkey",
        "zipCode": "34732",
        "ip": "85.34.78.112"
    },
    "shippingAddress": {
        "address": "Nidakule Göztepe, Merdivenköy Mah. Bora Sok. No:1",
        "zipCode": "34742",
        "contactName": "Jane Doe",
        "city": "Istanbul",
        "country": "Turkey"
    },
    "billingAddress": {
        "address": "Nidakule Göztepe, Merdivenköy Mah. Bora Sok. No:1",
        "zipCode": "34742",
        "contactName": "Jane Doe",
        "city": "Istanbul",
        "country": "Turkey"
    },

and latest part of json coming from another query

"basketItems": [
        {
            "id": "BI101",
            "price": "0.3",
            "name": "Binocular",
            "category1": "Collectibles",
            "category2": "Accessories",
            "itemType": "PHYSICAL"
        },
        {
            "id": "BI102",
            "price": "0.5",
            "name": "Game code",
            "category1": "Game",
            "category2": "Online Game Items",
            "itemType": "VIRTUAL"
        },
        {
            "id": "BI103",
            "price": "0.2",
            "name": "Usb",
            "category1": "Electronics",
            "category2": "Usb / Cable",
            "itemType": "PHYSICAL"
        }
    ],

I have to create Authorization token with api key api screet and requested body

Retrieve Installment Authorization Example
This is an example for Authorization

Api Key       : sandbox-afXhZPW0MQlE4dCUUlHcEopnMBgXnAZI
Secret Key    : sandbox-wbwpzKIiplZxI3hh5ALI4FJyAcZKL6kq

Request Body : “{ “locale”: “tr”, “conversationId”: “123456789”, “price”:“100.0”, “binNumber”:“554960” }”

PKI String : [locale=tr,conversationId=123456789,binNumber=554960,price=100.0]

Hash String : sandbox-afXhZPW0MQlE4dCUUlHcEopnMBgXnAZIx-iyzi-rndsandbox-wbwpzKIiplZxI3hh5ALI4FJyAcZKL6kq[locale=tr,conversationId=123456789,binNumber=554960,price=100.0]

Hash : GUIuVgX2o702mUHFi/s7TKQUTEI=

Authorization : IYZWS sandbox-afXhZPW0MQlE4dCUUlHcEopnMBgXnAZI:GUIuVgX2o702mUHFi/s7TKQUTEI=

Are there any idea @patrick ?

Can you guide me @George @patrick

I think you have the same issue as me.

I think the JSON for the request could be build with the data you have, but the authorization will be a problem.

In the input data of the API Action you can put all the root properties and they can have dynamic data, so the basketItems result can be applied there. Setting the Data Type to JSON would then generate the required JSON request.

The authorization requires a hash with the whole request in it, this can be a special PKI string or the stringified JSON from what I understand. Generating this will be a challenge and I don’t think this is possible with the current actions and formatters in Server Connect.

You use PHP, that is relative simple to edit and extend. You could add your own formatters. Add an extra file in dmxConnectLib/lib/formatters. The name of the file doesn’t matter, call it custom.php or so.

<?php

namespace lib\core;

// formatter merge
function formatter_merge($val, $data) {
    return array_merge((array)$value, (array)$data);
}

// formatter generateHash
function formatter_generateHash($val) {
    return base64_encode(sha1($val, true));
}

Formatters are called like {{ query1.merge(query2) }} and {{ apiKey + randomString + secretKey + pkiString ).generateHash() }}.

6 Likes

thanks for solution @patrick

 // formatter merge
function formatter_merge($val, $data) {
    return array_merge((array)$value, (array)$data);
}

you are using $value but it is $val .Maybe it is syntax error

// formatter merge
function formatter_merge($val, $data) {
    return array_merge((array)$val, (array)$data);
}

is this correct ?

Yes, $value should be $val. You can add as many functions you like, the first parameter is always the value that is passed from the expression, the part before the formatter. The name of the formatter is the same as the function name without the first formatter_.

1 Like

thank you so much . You solved a big problem. This usage will solve a lot of things. Maybe if you write a similar solution for users who use .net or asp, it will be very good news for asp and .net users too.

thanks again . I am very happy now :slight_smile:

That’s already possible using the Server Connect Array List component:

This topic was automatically closed after 26 hours. New replies are no longer allowed.