#1 2020-07-18 05:00:20

ertank
Member
Registered: 2016-03-16
Posts: 168

Help needed with JSON

Hello,

I am trying to make an integration with an online food sale chain. They have kind of weird design. I am getting a JSON (part of it) as below:

                "optionCategories": [
                    {
                        "optionCategory": "5dd28622e0450549407168f4",
                        "name": {
                            "tr": "1. Tantuni Tercihi",
                            "en": "1. Tantuni Tercihi"
                        },
                        "options": [
                            {
                                "option": "5dd28622e045052fb27168f6",
                                "product": "5dd28614c424f0658e8cedb8",
                                "name": {
                                    "tr": "Yarım Ekmek Et Tantuni",
                                    "en": "Yarım Ekmek Et Tantuni"
                                },
                                "price": 0,
                                "optionCategories": [
                                    {
                                        "optionCategory": "5dd28614c424f010da8cedb9",
                                        "name": {
                                            "tr": "Porsiyon Tercihi",
                                            "en": "Porsiyon Tercihi"
                                        },
                                        "options": [
                                            {
                                                "option": "5dd28614c424f05bd58cedbc",
                                                "name": {
                                                    "tr": "1 Porsiyon",
                                                    "en": "1 Porsiyon"
                                                },
                                                "price": 0
                                            }
                                        ]
                                    }
                                ]
                            }
                        ]

I could not figure how I should be preparing my record structure in order to be able to de-serialize something like that. I am actually stuck where "optionCategories" is becoming recursive. And there is another part where it is not going recursive. So, it is not always it is recursive, but sometimes. Here is an example for not recursive part (this also comes in identical array):

                    {
                        "optionCategory": "5dd28622e04505a7b37168ee",
                        "name": {
                            "tr": "İçecek Tercihi",
                            "en": "İçecek Tercihi"
                        },
                        "options": [
                            {
                                "option": "5dd28622e045052a3f7168ef",
                                "product": "5dd2861f35e9bba43d19b039",
                                "name": {
                                    "tr": "Eker Ayran (300 ml)",
                                    "en": "Eker Ayran (300 ml)"
                                },
                                "price": 0
                            }
                        ]
                    }

My current record structure (part of it) is not working and is as following:

  TName = packed record
    tr: string;
    en: string;
  end;

  TOption = packed record
    option: string;
    product: string;
    name: TName;
    price: Double;
  end;
  TOptions = TArray<TOption>;

  TOptionCategory = packed record
    optionCategory: string;
    name: TName;
    options: TOptions;
  end;
  TOptionCategories = TArray<TOptionCategory>;

I really do not want to parse whole JSON manually.

I appreciate any help.

Thanks & Regards,
Ertan

Last edited by ertank (2020-07-18 05:06:34)

Offline

#2 2020-07-18 07:41:36

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,655
Website

Re: Help needed with JSON

With records, in pascal (this is not a mORMot thing), you can't have recursion. And you can't have forward definition.

What you can do is define a TOptionCategoryNested record, then use it within your TOption.
Or use a RawJSON field, then use a method to unserialize the nested array.

Please follow the forum rules, and don't put to much code directly in the forum threads.

Offline

#3 2020-07-20 07:24:34

igors233
Member
Registered: 2012-09-10
Posts: 241

Re: Help needed with JSON

Consider using TDocVariantData instead, it will load any (correct) json no matter what structure it has. From there you could manually traverse items and use it directly where data is needed or move it to your records first and then use those records.

Offline

Board footer

Powered by FluxBB