I'm trying to separate a json into several pieces n times depending on the size of the json and the limit of characters within a field. For example, if the json occupies 16000 characters I divide it by 4000 (which is the limit per field) and thus divide it into 4 pieces type string[0], string[1],string[2],string[3] and then print them to the screen.
I have this code but it always gives me the following error:
Error: Index and length must refer to a location within the string. Parameter name: length
var splitA = [];
function separatedFields(nCharacter, valueDeInfo) {
var limit = 4000;
var split = [];
var nfield = Math.ceil((nCharacter) / limit);
var begin = 0;
var end = 4000;
for (i = 0; i < nfield; i++) {
if (i == nfield - 1) {
split[i] = valueDeInfo.slice(begin, end);
begin = end;
end = nCharacter;
} else {
split[i] = valueDeInfo.slice(begin, end);
begin = end;
end = end + 4000;
}
}
return split;
}
splitA = separatedFields(nCharacter, valueDeInfo);
Write("ARRAY FIRST 4000: " + splitA[0]);
Example of input/output:
valueDeInfo = ['{"Status":"OK","RequestID":"e5517d4e-ee43-451a-bbe8-09f9fc714c21","Results":[{"CustomerKey":null,"Name":"Control","ObjectID":null,"FieldType":"Number","IsPrimaryKey":false,"MaxLength":0,"Ordinal":3,"DefaultValue":"1","DataExtension":null,"StorageType":"Unspecified","DataType":null,"ValueType":"xsdstring","PropertyType":"string","IsCreatable":false,"IsUpdatable":false,"IsRetrievable":false,"IsQueryable":false,"IsFilterable":false,"IsPartnerProperty":false,"IsAccountProperty":false,"PartnerMap":null,"AttributeMaps":null,"Markups":null,"Precision":0,"Scale":0,"Label":null,"Description":null,"MinLength":0,"MinValue":null,"MaxValue":null,"IsRequired":false,"IsViewable":false,"IsEditable":false,"IsNillable":false,"IsRestrictedPicklist":false,"PicklistItems":null,"IsSendTime":false,"DisplayOrder":0,"References":null,"RelationshipName":null,"Status":null,"IsContextSpecific":false,"Client":null,"PartnerKey":null,"PartnerProperties":null,"CreatedDate":"0001-01-01T00:00:00.000","ModifiedDate":null,"ID":0,"Owner":null,"CorrelationID":null,"ObjectState":null,"IsPlatformObject":false}, {"CustomerKey":null,"Name":"CategoryID","ObjectID":null,"FieldType":"Number","IsPrimaryKey":false,"MaxLength":0,"Ordinal":8,"DefaultValue":"","DataExtension":null,"StorageType":"Unspecified","DataType":null,"ValueType":"xsdstring","PropertyType":"string","IsCreatable":false,"IsUpdatable":false,"IsRetrievable":false,"IsQueryable":false,"IsFilterable":false,"IsPartnerProperty":false,"IsAccountProperty":false,"PartnerMap":null,"AttributeMaps":null,"Markups":null,"Precision":0,"Scale":0,"Label":null,"Description":null,"MinLength":0,"MinValue":null,"MaxValue":null,"IsRequired":false,"IsViewable":false,"IsEditable":false,"IsNillable":false,"IsRestrictedPicklist":false,"PicklistItems":null,"IsSendTime":false,"DisplayOrder":0,"References":null,"RelationshipName":null,"Status":null,"IsContextSpecific":false,"Client":null,"PartnerKey":null,"PartnerProperties":null,"CreatedDate":"0001-01-01T00:00:00.000","ModifiedDate":null,"ID":0,"Owner":null,"CorrelationID":null,"ObjectState":null,"IsPlatformObject":false}, {"CustomerKey":null,"Name":"IsSendable","ObjectID":null,"FieldType":"Boolean","IsPrimaryKey":false,"MaxLength":0,"Ordinal":2,"DefaultValue":"","DataExtension":null,"StorageType":"Unspecified","DataType":null,"ValueType":"xsdstring","PropertyType":"string","IsCreatable":false,"IsUpdatable":false,"IsRetrievable":false,"IsQueryable":false,"IsFilterable":false,"IsPartnerProperty":false,"IsAccountProperty":false,"PartnerMap":null,"AttributeMaps":null,"Markups":null,"Precision":0,"Scale":0,"Label":null,"Description":null,"MinLength":0,"MinValue":null,"MaxValue":null,"IsRequired":false,"IsViewable":false,"IsEditable":false,"IsNillable":false,"IsRestrictedPicklist":false,"PicklistItems":null,"IsSendTime":false,"DisplayOrder":0,"References":null,"RelationshipName":null,"Status":null,"IsContextSpecific":false,"Client":null,"PartnerKey":null,"PartnerProperties":null,"CreatedDate":"0001-01-01T00:00:00.000","ModifiedDate":null,"ID":0,"Owner":null,"CorrelationID":null,"ObjectState":null,"IsPlatformObject":false}, {"CustomerKey":null,"Name":"Name","ObjectID":null,"FieldType":"Text","IsPrimaryKey":true,"MaxLength":50,"Ordinal":0,"DefaultValue":"","DataExtension":null,"StorageType":"Unspecified","DataType":null,"ValueType":"xsdstring","PropertyType":"string","IsCreatable":false,"IsUpdatable":false,"IsRetrievable":false,"IsQueryable":false,"IsFilterable":false,"IsPartnerProperty":false,"IsAccountProperty":false,"PartnerMap":null,"AttributeMaps":null,"Markups":null,"Precision":0,"Scale":0,"Label":null,"Description":null,"MinLength":0,"MinValue":null,"MaxValue":null,"IsRequired":true,"IsViewable":false,"IsEditable":false,"IsNillable":false,"IsRestrictedPicklist":false,"PicklistItems":null,"IsSendTime":false,"DisplayOrder":0,"References":null,"RelationshipName":null,"Status":null,"IsContextSpecific":false,"Client":null,"PartnerKey":null,"PartnerProperties":null,"CreatedDate":"0001-01-01T00:00:00.000","ModifiedDate":null,"ID":0,"Owner":null,"CorrelationID":null,"ObjectState":null,"IsPlatformObject":false}, {"CustomerKey":null,"Name":"IsRequired","ObjectID":null,"FieldType":"Boolean","IsPrimaryKey":false,"MaxLength":0,"Ordinal":10,"DefaultValue":"","DataExtension":null,"StorageType":"Unspecified","DataType":null,"ValueType":"xsdstring","PropertyType":"string","IsCreatable":false,"IsUpdatable":false,"IsRetrievable":false,"IsQueryable":false,"IsFilterable":false,"IsPartnerProperty":false,"IsAccountProperty":false,"PartnerMap":null,"AttributeMaps":null,"Markups":null,"Precision":0,"Scale":0,"Label":null,"Description":null,"MinLength":0,"MinValue":null,"MaxValue":null,"IsRequired":false,"IsViewable":false,"IsEditable":false,"IsNillable":false,"IsRestrictedPicklist":false,"PicklistItems":null,"IsSendTime":false,"DisplayOrder":0,"References":null,"RelationshipName":null,"Status":null,"IsContextSpecific":false,"Client":null,"PartnerKey":null,"PartnerProperties":null,"CreatedDate":"0001-01-01T00:00:00.000","ModifiedDate":null,"ID":0,"Owner":null,"CorrelationID":null,"ObjectState":null,"IsPlatformObject":false}, {"CustomerKey":null,"Name":"infoDE3","ObjectID":null,"FieldType":"Text","IsPrimaryKey":false,"MaxLength":4000,"Ordinal":18,"DefaultValue":"","DataExtension":null,"StorageType":"Unspecified","DataType":null,"ValueType":"xsdstring","PropertyType":"string","IsCreatable":false,"IsUpdatable":false,"IsRetrievable":false,"IsQueryable":false,"IsFilterable":false,"IsPartnerProperty":false,"IsAccountProperty":false,"PartnerMap":null,"AttributeMaps":null,"Markups":null,"Precision":0,"Scale":0,"Label":null,"Description":null,"MinLength":0,"MinValue":null,"MaxValue":null,"IsRequired":true,"IsViewable":false,"IsEditable":false,"IsNillable":false,"IsRestrictedPicklist":false,"PicklistItems":null,"IsSendTime":false,"DisplayOrder":0,"References":null,"RelationshipName":null,"Status":null,"IsContextSpecific":false,"Client":null,"PartnerKey":null,"PartnerProperties":null,"CreatedDate":"0001-01-01T00:00:00.000","ModifiedDate":null,"ID":0,"Owner":null,"CorrelationID":null,"ObjectState":null,"IsPlatformObject":false}, {"CustomerKey":null,"Name":"Description","ObjectID":null,"FieldType":"Text","IsPrimaryKey":false,"MaxLength":4000,"Ordinal":6,"DefaultValue":"","DataExtension":null,"StorageType":"Unspecified","DataType":null,"ValueType":"xsdstring","PropertyType":"string","IsCreatable":false,"IsUpdatable":false,"IsRetrievable":false,"IsQueryable":false,"IsFilterable":false,"IsPartnerProperty":false,"IsAccountProperty":false,"PartnerMap":null,"AttributeMaps":null,"Markups":null,"Precision":0,"Scale":0,"Label":null,"Description":null,"MinLength":0,"MinValue":null,"MaxValue":null,"IsRequired":false,"IsViewable":false,"IsEditable":false,"IsNillable":false,"IsRestrictedPicklist":false,"PicklistItems":null,"IsSendTime":false,"DisplayOrder":0,"References":null,"RelationshipName":null,"Status":null,"IsContextSpecific":false,"Client":null,"PartnerKey":null,"PartnerProperties":null,"CreatedDate":"0001-01-01T00:00:00.000","ModifiedDate":null,"ID":0,"Owner":null,"CorrelationID":null,"ObjectState":null,"IsPlatformObject":false}, {"CustomerKey":null,"Name":"ModifiedDate","ObjectID":null,"FieldType":"Date","IsPrimaryKey":false,"MaxLength":0,"Ordinal":7,"DefaultValue":"","DataExtension":null,"StorageType":"Unspecified","DataType":null,"ValueType":"xsdstring","PropertyType":"string","IsCreatable":false,"IsUpdatable":false,"IsRetrievable":false,"IsQueryable":false,"IsFilterable":false,"IsPartnerProperty":false,"IsAccountProperty":false,"PartnerMap":null,"AttributeMaps":null,"Markups":null,"Precision":0,"Scale":0,"Label":null,"Description":null,"MinLength":0,"MinValue":null,"MaxValue":null,"IsRequired":false,"IsViewable":false,"IsEditable":false,"IsNillable":false,"IsRestrictedPicklist":false,"PicklistItems":null,"IsSendTime":false,"DisplayOrder":0,"References":null,"RelationshipName":null,"Status":null,"IsContextSpecific":false,"Client":null,"PartnerKey":null,"PartnerProperties":null,"CreatedDate":"0001-01-01T00:00:00.000","ModifiedDate":null,"ID":0,"Owner":null,"CorrelationID":null,"ObjectState":null,"IsPlatformObject":false}']
nCharacters = valueDeinfo.length;
The correct output would be:
{"Status":"OK","RequestID":"e5517d4e-ee43-451a-bbe8-09f9fc714c21","Results":[{"CustomerKey":null,"Name":"Control","ObjectID":null,"FieldType":"Number","IsPrimaryKey":false,"MaxLength":0,"Ordinal":3,"DefaultValue":"1","DataExtension":null,"StorageType":"Unspecified","DataType":null,"ValueType":"xsdstring","PropertyType":"string","IsCreatable":false,"IsUpdatable":false,"IsRetrievable":false,"IsQueryable":false,"IsFilterable":false,"IsPartnerProperty":false,"IsAccountProperty":false,"PartnerMap":null,"AttributeMaps":null,"Markups":null,"Precision":0,"Scale":0,"Label":null,"Description":null,"MinLength":0,"MinValue":null,"MaxValue":null,"IsRequired":false,"IsViewable":false,"IsEditable":false,"IsNillable":false,"IsRestrictedPicklist":false,"PicklistItems":null,"IsSendTime":false,"DisplayOrder":0,"References":null,"RelationshipName":null,"Status":null,"IsContextSpecific":false,"Client":null,"PartnerKey":null,"PartnerProperties":null,"CreatedDate":"0001-01-01T00:00:00.000","ModifiedDate":null,"ID":0,"Owner":null,"CorrelationID":null,"ObjectState":null,"IsPlatformObject":false}, {"CustomerKey":null,"Name":"CategoryID","ObjectID":null,"FieldType":"Number","IsPrimaryKey":false,"MaxLength":0,"Ordinal":8,"DefaultValue":"","DataExtension":null,"StorageType":"Unspecified","DataType":null,"ValueType":"xsdstring","PropertyType":"string","IsCreatable":false,"IsUpdatable":false,"IsRetrievable":false,"IsQueryable":false,"IsFilterable":false,"IsPartnerProperty":false,"IsAccountProperty":false,"PartnerMap":null,"AttributeMaps":null,"Markups":null,"Precision":0,"Scale":0,"Label":null,"Description":null,"MinLength":0,"MinValue":null,"MaxValue":null,"IsRequired":false,"IsViewable":false,"IsEditable":false,"IsNillable":false,"IsRestrictedPicklist":false,"PicklistItems":null,"IsSendTime":false,"DisplayOrder":0,"References":null,"RelationshipName":null,"Status":null,"IsContextSpecific":false,"Client":null,"PartnerKey":null,"PartnerProperties":null,"CreatedDate":"0001-01-01T00:00:00.000","ModifiedDate":null,"ID":0,"Owner":null,"CorrelationID":null,"ObjectState":null,"IsPlatformObject":false}, {"CustomerKey":null,"Name":"IsSendable","ObjectID":null,"FieldType":"Boolean","IsPrimaryKey":false,"MaxLength":0,"Ordinal":2,"DefaultValue":"","DataExtension":null,"StorageType":"Unspecified","DataType":null,"ValueType":"xsdstring","PropertyType":"string","IsCreatable":false,"IsUpdatable":false,"IsRetrievable":false,"IsQueryable":false,"IsFilterable":false,"IsPartnerProperty":false,"IsAccountProperty":false,"PartnerMap":null,"AttributeMaps":null,"Markups":null,"Precision":0,"Scale":0,"Label":null,"Description":null,"MinLength":0,"MinValue":null,"MaxValue":null,"IsRequired":false,"IsViewable":false,"IsEditable":false,"IsNillable":false,"IsRestrictedPicklist":false,"PicklistItems":null,"IsSendTime":false,"DisplayOrder":0,"References":null,"RelationshipName":null,"Status":null,"IsContextSpecific":false,"Client":null,"PartnerKey":null,"PartnerProperties":null,"CreatedDate":"0001-01-01T00:00:00.000","ModifiedDate":null,"ID":0,"Owner":null,"CorrelationID":null,"ObjectState":null,"IsPlatformObject":false}, {"CustomerKey":null,"Name":"Name","ObjectID":null,"FieldType":"Text","IsPrimaryKey":true,"MaxLength":50,"Ordinal":0,"DefaultValue":"","DataExtension":null,"StorageType":"Unspecified","DataType":null,"ValueType":"xsdstring","PropertyType":"string","IsCreatable":false,"IsUpdatable":false,"IsRetrievable":false,"IsQueryable":false,"IsFilterable":false,"IsPartnerProperty":false,"IsAccountProperty":false,"PartnerMap":null,"AttributeMaps":null,"Markups":null,"Precision":0,"Scale":0,"Label":null,"Description":null,"MinLength":0,"MinValue":null,"MaxValue":null,"IsRequired":true,"IsViewable":false,"IsEditable":false,"IsNillable":false,"IsRestrictedPicklist":false,"PicklistItems":null,"IsSendTime":false,"DisplayOrder":0,"References":null,"RelationshipName":null,"Status":null,"IsContextSpecific":false,"Client":null,"PartnerKey":null,"PartnerProperties":null,"CreatedDate":"0001-01-0
Basically, in splitA[0] should be the first 4000 characters, in split[1] should be in the next 4000 characters and so on until you reach nCharacters
So you basically just want to split a string at a given number of characters?
You can try a regex for that
const splitAtInterval = (string, interval) => {
const r = new RegExp(`.{1,${interval}}`, 'g')
return string.match(r);
}
var text = "Some random string (in your case your JSON string)";
console.log(splitAtInterval(text, 4));
There are other options, just search google for "split string after x characters". for example https://bobbyhadz.com/blog/javascript-split-string-substrings-n-characters
Obviously, as mentioned in the comments, those strings will not be valid json anymore. You will have to fetch all the entries again and connect them via join.
I'm beginner in JS. I've tried to understand Caesar Cipher ROT13, but it was too complicated for me. So I've tried to write my own code. Here it is below:
function encrip() {
var alphabet = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"];
var str = "Ni Hao";
var string = str.toUpperCase();
for (var i = 0; i < string.length; i++) {
for (var k = 0; k < alphabet.length; k++) {
if(string.charAt(i) == alphabet[k]) {
/* console.log(string.charAt(i) + ' ' + alphabet.indexOf(alphabet[k])); */
}
}
}
}
encrip();
But I am stuck. How to do:
1. Get value from var str and then access to var alphabet , after change each letter from var str value to next 3 from alphabet (var str each element's current position would be changed) For example: Input: Ni Hao ==> output: QL KDR
2. Create universal code, I mean, not only for changing position by 3, but when I give value '5', each element would be changed by next 5 positions from alphabet. So output can be changed when I change its' value
I hope I explained everything clearly. Thanks everyone in advance for help!!
you can use the following function to encrypt english words, the 1st parameter is the string to encrypt and the 2nd for shifting
function encryp(str,pos){
var alpha="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var strUC=str.toUpperCase();
var enc="";
for(var i=0;i<strUC.length;i++){
if(strUC.charAt(i)!=" "){
enc+=alpha.charAt((alpha.indexOf(strUC.charAt(i))+pos)%26)
}
else{
enc+=" "
}
// in your case pos=3
}
return enc;
}
console.log(encryp("NiHao",3));
You don't need two for loops to do this. Iterate over the input string and find the index of each character in the alphabet array, if found add the shift to it to get the encrypted character.
To handle overflow use the modulus operator to cycle through the array.
Also I assume that you are not going use any special symbols to do the encryption.
function encrip(string, shift) {
var alphabet = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"];
string = string.toUpperCase();
let arr = [];
for (var i = 0; i < string.length; i++) {
let char = alphabet.indexOf(string[i]) !== -1 ? alphabet[(alphabet.indexOf(string[i]) %26) + shift] : " ";
arr.push(char);
}
let encryp = arr.join("");
console.log(encryp);
return encryp;
}
encrip("Ni Hao", 3);
First of all, instead of your inner for loop scanning the whole alphabet array, you can use the built-in function indexOf:
alphabet.indexOf('K') // returns 10
Secondly, you'll want to build up your enciphered string in a separate variable. For each letter, get the index of that letter in the alphabet, add your cipher offset parameter to that index and add the resulting letter from the alphabet to your new string. An important step is that when you add to the index of the letter, you want to make sure the resulting index is within range for the alphabet array. You can do that using the % (modulo) operator, which will wrap high values back round to the start of the array. In full:
function encipher(input, offset) {
var alphabet = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"];
var str = input.toUpperCase();
var result = '';
for (var i = 0; i < str.length; i++) {
letterIndex = alphabet.indexOf(str.charAt(i));
if (letterIndex === -1) {
result += str[i]; // if the letter isn't found in the alphabet, add it to the result unchanged
continue;
}
cipheredIndex = (letterIndex + offset) % alphabet.length; // wrap index to length of alphabet
result += alphabet[cipheredIndex];
}
console.log(result);
}
encipher('Ni Hao', 5); // output: 'SN MFT'