1

I have a string from encoded url like this:

leads%5Bupdate%5D%5B0%5D%5Bid%5D=123432456&leads%5Bupdate%5D%5B0%5D%5Bname%5D=%D0%A1%D0%B4%D0%B5%D0%BB%D0%BA%D0%B0+2342345345345&leads%5Bupdate%5D%5B0%5D%5Bstatus_id%5D=45645676&leads%5Bupdate%5D%5B0%5D%5Bprice%5D=0&leads%5Bupdate%5D%5B0%5D%5Bresponsible_user_id%5D=2423432&leads%5Bupdate%5D%5B0%5D%5Blast_modified%5D=1231423556&leads%5Bupdate%5D%5B0%5D%5Bmodified_user_id%5D=6756756&leads%5Bupdate%5D%5B0%5D%5Bcreated_user_id%5D=2423432&leads%5Bupdate%5D%5B0%5D%5Bdate_create%5D=1234534678&leads%5Bupdate%5D%5B0%5D%5Bpipeline_id%5D=4564564&leads%5Bupdate%5D%5B0%5D%5Baccount_id%5D=34578987&leads%5Bupdate%5D%5B0%5D%5Bcustom_fields%5D%5B0%5D%5Bid%5D=567834&leads%5Bupdate%5D%5B0%5D%5Bcustom_fields%5D%5B0%5D%5Bname%5D=MANGO+OFFICE+%D0%9D%D0%BE%D0%BC%D0%B5%D1%80+%D0%BB%D0%B8%D0%BD%D0%B8%D0%B8&leads%5Bupdate%5D%5B0%5D%5Bcustom_fields%5D%5B0%5D%5Bvalues%5D%5B0%5D%5Bvalue%5D=74994567456&leads%5Bupdate%5D%5B0%5D%5Bcreated_at%5D=1234534678&leads%5Bupdate%5D%5B0%5D%5Bupdated_at%5D=1231423556&account%5Bsubdomain%5D=google&account%5Bid%5D=34578987&account%5B_links%5D%5Bself%5D=https%3A%2F%2Fgoogle.site.com

decoded string:

leads[update][0][id]=123432456&leads[update][0][name]=Сделка+2342345345345&leads[update][0][status_id]=45645676&leads[update][0][price]=0&leads[update][0][responsible_user_id]=2423432&leads[update][0][last_modified]=1231423556&leads[update][0][modified_user_id]=6756756&leads[update][0][created_user_id]=2423432&leads[update][0][date_create]=1234534678&leads[update][0][pipeline_id]=4564564&leads[update][0][account_id]=34578987&leads[update][0][custom_fields][0][id]=567834&leads[update][0][custom_fields][0][name]=MANGO+OFFICE+Номер+линии&leads[update][0][custom_fields][0][values][0][value]=74994567456&leads[update][0][created_at]=1234534678&leads[update][0][updated_at]=1231423556&account[subdomain]=google&account[id]=34578987&account[_links][self]=https://google.site.com

array:

[
    'leads' => [
        'update' => [
            [
                'id' => 22652446,
                'name' => 'Сделка 74956462949',
                'status_id' => 33687064,
                'price' => 0,
                'responsible_user_id' => 5827756,
                'last_modified' => 1675346949,
                'modified_user_id' => 8775297,
                'created_user_id' => 5827756,
                'date_create' => 1640068981,
                'pipeline_id' => 3365869,
                'account_id' => 28791871,
                'custom_fields' => [
                    [
                        'id' => 662156,
                        'name' => 'MANGO OFFICE Номер линии',
                        'values' => [
                            [
                                'value' => 74994551765,
                            ],
                        ],
                    ],
                ],
                'created_at' => 1640068981,
                'updated_at' => 1675346949,
            ],
        ],
    ],
    'account' => [
        'subdomain' => 'google',
        'id' => 28791871,
        '_links' => [
            'self' => 'https://google.site.com'
        ],
    ],
]

and JSON:

{
    "leads":
    {
        "update": [
            {
                "id":22652446,
                "name":"\u0421\u0434\u0435\u043b\u043a\u0430 74956462949",
                "status_id":33687064,
                "price":0,
                "responsible_user_id":5827756,
                "last_modified":1675346949,
                "modified_user_id":8775297,
                "created_user_id":5827756,
                "date_create":1640068981,
                "pipeline_id":3365869,
                "account_id":28791871,
                "custom_fields": [
                    {
                        "id":662156,
                        "name":"MANGO OFFICE \u041d\u043e\u043c\u0435\u0440 \u043b\u0438\u043d\u0438\u0438",
                        "values": [
                            {
                                "value":74994551765
                            }
                        ]
                    }
                ],
                "created_at":1640068981,
                "updated_at":1675346949
            }
        ]
    },
    "account":
    {
        "subdomain":"google",
        "id":28791871,
        "_links":
        {
            "self":"https:\/\/google.site.com"
        }
    }
}

How I can convert it to JSON with Groovy?

Thanks.

P.S: I read this Convert url encoded data to json but it's not help.

Andrej Istomin
  • 2,527
  • 2
  • 15
  • 22
  • obviously you have to split string by `&`, then separate key-value by `=`, then apply UrlDecode to value. BUT: this kind of accessor `leads[update][0][id]=123432456` is not groovysh enough to expand left part to object/array structure. – daggett Feb 03 '23 at 14:56
  • It's not help me because array/object can be more than `leads[update][0][id]=123432456`. For example `leads[update][0][custom_fields]`. So simple way which I see convert string to array and then to JSON. But how I can do it? – Valentin Borisenko Feb 03 '23 at 16:10
  • i'm saying that this is not a kind of standard groovy notation. so, you have to do a custom parser for non-standard format. – daggett Feb 03 '23 at 16:18

0 Answers0