Home > other >  Can you please tell me what are the problems with parsing, json?
Can you please tell me what are the problems with parsing, json?

Time:12-02

Good afternoon, please tell me what the error could be, I receive a request in this form, I completely removed the link because of the '=' symbol, but I continue to receive an error, reviewing all the values ​​received, but I could not find a conflict.

I attach the script that I am using

function teststr (){
     var test = "leads[update][0][id]=30986646&leads[update][0][name]=Елизавета (Дмитрий) 26.11.2021 18:00&leads[update][0][status_id]=33131353&leads[update][0][old_status_id]=33131347&leads[update][0][price]=0&leads[update][0][responsible_user_id]=7442937&leads[update][0][last_modified]=1637945719&leads[update][0][modified_user_id]=0&leads[update][0][created_user_id]=0&leads[update][0][date_create]=1637694044&leads[update][0][pipeline_id]=1635139&leads[update][0][tags][0][id]=451580&leads[update][0][tags][0][name]=tilda&leads[update][0][tags][1][id]=469607&leads[update][0][tags][1][name]=Вводное&leads[update][0][tags][2][id]=475269&leads[update][0][tags][2][name]=Партнерка&leads[update][0][tags][3][id]=480372&leads[update][0][tags][3][name]=ВУ&leads[update][0][tags][4][id]=483228&leads[update][0][tags][4][name]=Акция +2&leads[update][0][tags][5][id]=485606&leads[update][0][tags][5][name]=Посоветоваться с мужем&leads[update][0][tags][6][id]=487238&leads[update][0][tags][6][name]=Мобильный&leads[update][0][tags][7][id]=490756&leads[update][0][tags][7][name]=Чтение&leads[update][0][tags][8][id]=493262&leads[update][0][tags][8][name]=неделя в подарок&leads[update][0][tags][9][id]=493402&leads[update][0][tags][9][name]=людаговорушкина&leads[update][0][account_id]=24809335&leads[update][0][custom_fields][0][id]=683303&leads[update][0][custom_fields][0][name]=utm_referrer&leads[update][0][custom_fields][0][code]=UTM_REFERRER&leads[update][0][custom_fields][1][id]=681646&leads[update][0][custom_fields][1][name]=Сотрудник, Yclients&leads[update][0][custom_fields][1][values][0][value]=Лапина Марина&leads[update][0][custom_fields][2][id]=681648&leads[update][0][custom_fields][2][name]=Длительность процедуры, Yclients&leads[update][0][custom_fields][2][values][0][value]=45 мин.&leads[update][0][custom_fields][3][id]=928932&leads[update][0][custom_fields][3][name]=Менеджер 1 линии&leads[update][0][custom_fields][3][values][0][value]=Кочетова Ольга&leads[update][0][custom_fields][3][values][0][enum]=4204970&leads[update][0][custom_fields][4][id]=678201&leads[update][0][custom_fields][4][name]=Регион&leads[update][0][custom_fields][4][values][0][value]=Краснодарский край&leads[update][0][custom_fields][5][id]=913962&leads[update][0][custom_fields][5][name]=Возраст ребенка&leads[update][0][custom_fields][5][values][0][value]=5&leads[update][0][custom_fields][5][values][0][enum]=4198692&leads[update][0][custom_fields][6][id]=673089&leads[update][0][custom_fields][6][name]=ID записи, Yclients&leads[update][0][custom_fields][6][values][0][value]=290330-382828982&leads[update][0][custom_fields][7][id]=673093&leads[update][0][custom_fields][7][name]=Дата записи, Yclients&leads[update][0][custom_fields][7][values][0][value]=26.11.2021&leads[update][0][custom_fields][8][id]=673091&leads[update][0][custom_fields][8][name]=Время записи, Yclients&leads[update][0][custom_fields][8][values][0][value]=18:00&leads[update][0][custom_fields][9][id]=928936&leads[update][0][custom_fields][9][name]=Требует дозвона (Т)&leads[update][0][custom_fields][9][values][0]=1637701200&leads[update][0][custom_fields][10][id]=928940&leads[update][0][custom_fields][10][name]=Подтвердили ВУ (Т)&leads[update][0][custom_fields][10][values][0]=1637874000&leads[update][0][custom_fields][11][id]=928942&leads[update][0][custom_fields][11][name]=Занятие прошло (Т)&leads[update][0][custom_fields][11][values][0]=1637874000&leads[update][0][custom_fields][12][id]=928944&leads[update][0][custom_fields][12][name]=Записан на пробное (Т)&leads[update][0][custom_fields][12][values][0]=1637787600&leads[update][0][custom_fields][13][id]=670612&leads[update][0][custom_fields][13][name]=REFERER&leads[update][0][custom_fields][13][values][0][value]=SSilka&leads[update][0][custom_fields][14][id]=680880&leads[update][0][custom_fields][14][name]=FORMNAME&leads[update][0][custom_fields][14][values][0][value]=форма&leads[update][0][custom_fields][14][code]=FORMNAME&leads[update][0][custom_fields][15][id]=671845&leads[update][0][custom_fields][15][name]=UTM_SOURCE&leads[update][0][custom_fields][15][values][0][value]=blogger&leads[update][0][custom_fields][16][id]=671847&leads[update][0][custom_fields][16][name]=UTM_MEDIUM&leads[update][0][custom_fields][16][values][0][value]=rg&leads[update][0][custom_fields][17][id]=671849&leads[update][0][custom_fields][17][name]=UTM_CAMPAIGN&leads[update][0][custom_fields][17][values][0][value]=nasty__tasty&leads[update][0][custom_fields][18][id]=670608&leads[update][0][custom_fields][18][name]=TRANID&leads[update][0][custom_fields][18][values][0][value]=2225828:2726134801&leads[update][0][custom_fields][19][id]=670610&leads[update][0][custom_fields][19][name]=FORMID&leads[update][0][custom_fields][19][values][0][value]=form319161715&leads[update][0][created_at]=1637694044&leads[update][0][updated_at]=1637945719&account[subdomain]=mylogoped&account[id]=24809335&account[_links][self]=https://mylogoped.amocrm.ru";
       var res = form2Json(test);
    Logger.log(res);
    }

SyntaxError: Unexpected end of JSON input (строка 42, файл form2Json)

function form2Json(formString) {
  'use strict';
  let obj;
  let i;
  let pt;
  let keys;
  let j;
  let ev;
  if (typeof form2Json.br !== 'function') {
    form2Json.br = function(repl) {
      if (repl.indexOf(']') !== -1) {
        return repl.replace(/\](. ?)(,|$)/g, function($1, $2, $3) {
          return form2Json.br($2   '}'   $3);
        });
      }
      return repl;
    };
  }
  formString =
    '{"'  
    (formString.indexOf('%') !== -1
      ? decodeURIComponent(formString)
      : formString)  
    '"}';
  obj = formString
    .replace(/\=/g, '":"')
    .replace(/&/g, '","')
    .replace(/\[/g, '":{"');
  obj = JSON.parse(
    obj.replace(/\](. ?)(,|$)/g, function($1, $2, $3) {
      return form2Json.br($2   '}'   $3);
    })
  );
  pt = ('&'   formString)
    .replace(/(\[|\]|\=)/g, '"$1"')
    .replace(/\]" /g, ']')
    .replace(/&([^\[\=] ?)(\[|\=)/g, '"&["$1]$2');
  pt = (pt   '"').replace(/^"&/, '').split('&');
  for (i = 0; i < pt.length; i  ) {
    ev = obj;
    keys = pt[i].match(/(?!:(\["))([^"] ?)(?=("\]))/g);
    for (j = 0; j < keys.length; j  ) {
      if (!Object.prototype.hasOwnProperty.call(ev, keys[j])) {
        if (keys.length > j   1) {
          ev[keys[j]] = {};
        } else {
          ev[keys[j]] = pt[i].split('=')[1].replace(/"/g, '');
          break;
        }
      }
      ev = ev[keys[j]];
    }
  }
  return obj;
}

Декодированный полученный запрос, подскажите пожалуйста, что я пропустил ?

leads[update][0][id]=30986646
leads[update][0][name]=Елизавета (Дмитрий) 26.11.2021 18:00
leads[update][0][status_id]=33131353
leads[update][0][old_status_id]=33131347
leads[update][0][price]=0
leads[update][0][responsible_user_id]=7442937
leads[update][0][last_modified]=1637945719
leads[update][0][modified_user_id]=0
leads[update][0][created_user_id]=0
leads[update][0][date_create]=1637694044
leads[update][0][pipeline_id]=1635139
leads[update][0][tags][0][id]=451580
leads[update][0][tags][0][name]=tilda
leads[update][0][tags][1][id]=469607
leads[update][0][tags][1][name]=Вводное
leads[update][0][tags][2][id]=475269
leads[update][0][tags][2][name]=Партнерка
leads[update][0][tags][3][id]=480372
leads[update][0][tags][3][name]=ВУ
leads[update][0][tags][4][id]=483228
leads[update][0][tags][4][name]=Акция  2
leads[update][0][tags][5][id]=485606
leads[update][0][tags][5][name]=Посоветоваться с мужем
leads[update][0][tags][6][id]=487238
leads[update][0][tags][6][name]=Мобильный
leads[update][0][tags][7][id]=490756
leads[update][0][tags][7][name]=Чтение
leads[update][0][tags][8][id]=493262
leads[update][0][tags][8][name]=неделя в подарок
leads[update][0][tags][9][id]=493402
leads[update][0][tags][9][name]=людаговорушкина
leads[update][0][account_id]=24809335
leads[update][0][custom_fields][0][id]=683303
leads[update][0][custom_fields][0][name]=utm_referrer
leads[update][0][custom_fields][0][code]=UTM_REFERRER
leads[update][0][custom_fields][1][id]=681646
leads[update][0][custom_fields][1][name]=Сотрудник, Yclients
leads[update][0][custom_fields][1][values][0][value]=Лапина Марина
leads[update][0][custom_fields][2][id]=681648
leads[update][0][custom_fields][2][name]=Длительность процедуры, Yclients
leads[update][0][custom_fields][2][values][0][value]=45 мин.
leads[update][0][custom_fields][3][id]=928932
leads[update][0][custom_fields][3][name]=Менеджер 1 линии
leads[update][0][custom_fields][3][values][0][value]=Кочетова Ольга
leads[update][0][custom_fields][3][values][0][enum]=4204970
leads[update][0][custom_fields][4][id]=678201
leads[update][0][custom_fields][4][name]=Регион
leads[update][0][custom_fields][4][values][0][value]=Краснодарский край
leads[update][0][custom_fields][5][id]=913962
leads[update][0][custom_fields][5][name]=Возраст ребенка
leads[update][0][custom_fields][5][values][0][value]=5
leads[update][0][custom_fields][5][values][0][enum]=4198692
leads[update][0][custom_fields][6][id]=673089
leads[update][0][custom_fields][6][name]=ID записи, Yclients
leads[update][0][custom_fields][6][values][0][value]=290330-382828982
leads[update][0][custom_fields][7][id]=673093
leads[update][0][custom_fields][7][name]=Дата записи, Yclients
leads[update][0][custom_fields][7][values][0][value]=26.11.2021
leads[update][0][custom_fields][8][id]=673091
leads[update][0][custom_fields][8][name]=Время записи, Yclients
leads[update][0][custom_fields][8][values][0][value]=18:00
leads[update][0][custom_fields][9][id]=928936
leads[update][0][custom_fields][9][name]=Требует дозвона (Т)
leads[update][0][custom_fields][9][values][0]=1637701200
leads[update][0][custom_fields][10][id]=928940
leads[update][0][custom_fields][10][name]=Подтвердили ВУ (Т)
leads[update][0][custom_fields][10][values][0]=1637874000
leads[update][0][custom_fields][11][id]=928942
leads[update][0][custom_fields][11][name]=Занятие прошло (Т)
leads[update][0][custom_fields][11][values][0]=1637874000
leads[update][0][custom_fields][12][id]=928944
leads[update][0][custom_fields][12][name]=Записан на пробное (Т)
leads[update][0][custom_fields][12][values][0]=1637787600
leads[update][0][custom_fields][13][id]=670612
leads[update][0][custom_fields][13][name]=REFERER
leads[update][0][custom_fields][13][values][0][value]=SSilka
leads[update][0][custom_fields][14][id]=680880
leads[update][0][custom_fields][14][name]=FORMNAME
leads[update][0][custom_fields][14][values][0][value]=форма
leads[update][0][custom_fields][14][code]=FORMNAME
leads[update][0][custom_fields][15][id]=671845
leads[update][0][custom_fields][15][name]=UTM_SOURCE
leads[update][0][custom_fields][15][values][0][value]=blogger
leads[update][0][custom_fields][16][id]=671847
leads[update][0][custom_fields][16][name]=UTM_MEDIUM
leads[update][0][custom_fields][16][values][0][value]=rg
leads[update][0][custom_fields][17][id]=671849
leads[update][0][custom_fields][17][name]=UTM_CAMPAIGN
leads[update][0][custom_fields][17][values][0][value]=nasty__tasty
leads[update][0][custom_fields][18][id]=670608
leads[update][0][custom_fields][18][name]=TRANID
leads[update][0][custom_fields][18][values][0][value]=2225828:2726134801
leads[update][0][custom_fields][19][id]=670610
leads[update][0][custom_fields][19][name]=FORMID
leads[update][0][custom_fields][19][values][0][value]=form319161715
leads[update][0][created_at]=1637694044
leads[update][0][updated_at]=1637945719
account[subdomain]=mylogoped
account[id]=24809335
account[_links][self]=https://mylogoped.amocrm.ru

CodePudding user response:

So, here is the 99% working solution.

Replace the line (22):

? decodeURIComponent(formString)

with

? decodeURI(formString)

And change the line:

ev[keys[j]] = pt[i].split('=')[1].replace(/"/g, '');

with

ev[keys[j]] = decodeURIComponent(pt[i].split('=')[1].replace(/"/g, ''));

It will get you correct JSON.

But I didn't manage to get the decoded value for URL. It still looks like "self":"https://mylogoped.amocrm.ru". Which is not a big deal I suppose.

  • Related