Pavel Valeriu Pavel Valeriu - 5 months ago 11
JSON Question

Dynamically add array elements to JSON Object

I'm creating a JSON object from an array and I want to dynamically push data to this JSON object based on the values from array. See my code for a better understanding of my problem...

for(i=0;i<duplicates.length; i++) {
var request = {
"name": duplicates[i].scope,
"id": 3,
"rules":[
{
"name": duplicates[i].scope + " " + "OP SDR Sync",
"tags": [
{
"tagId": 1,
"variables":[
{
"variable": duplicates[i].variable[j],
"matchType": "Regex",
"value": duplicates[i].scopeDef
}
],
"condition": false,
},
{
"tagId": 1,
"condition": false,
}
],
"ruleSetId": 3,
}
]
}
}


I take object properties from the
duplicates
array that can have the following elements:

[{scopeDef=.*, scope=Global, variable=[trackingcode, v1, v2]}, {scopeDef=^https?://([^/:\?]*\.)?delta.com/products, scope=Products Section, variable=[v3]}]


As you can see, an object contain
variable
element that can have multiple values. I need to push to the JSON object all those values dynamically (meaning that there could be more than 3 values in an array).

For example, after I push all the values from the
duplicates
array, my JSON object should look like this:

name=Products Section,
rules=
[
{
name=Products Section OP SDR Sync,
tags=[
{
variables=
[
{
matchType=Regex,
variable=v3,
value=^https?://([^/:\?]*\.)?delta.com/products
},
{
matchType=Regex,
variable=trackingcode,
value=.*
},
{
matchType=Regex,
variable=v1,
value=.*
},
{
matchType=Regex,
variable=v2,
value=.*
}
],
condition=false,
},
{
condition=false,
tagId=1
}
],
ruleSetId=3
}
]
}


I tried the following code but without success:

for(var j in duplicates[i].variable) {
var append = JSON.parse(request);
append['variables'].push({
"variable":duplicates[i].variable[j],
"matchType": "Regex",
"value": duplicates[i].scopeDef
})
}


Please let me know if I need to provide additional information, I just started working with JSON objects.

Teo Teo
Answer

First of all, you dont need to parse request, you already create an object, parse only when you get JSON as string, like:

var json='{"a":"1", "b":"2"}';
var x = JSON.parse(json);

Next, you have any property of object wrapped in arrays. To correctly work with it you should write:

request.rules[0].tags[0].variables.push({
  "variable":duplicates[i].variable[j],
  "matchType": "Regex",
  "value": duplicates[i].scopeDef
})

If you want to use your code snippet, you need some changes in request:

  var request = {
"name": duplicates[i].scope,
"id": 3,
"variables":[
   {
   "variable": duplicates[i].variable[j],
   "matchType": "Regex",
   "value": duplicates[i].scopeDef
   }
           ],
"rules":[
  {
    "name": duplicates[i].scope + " " + "OP SDR Sync",
    "tags": [
      {
        "tagId": 1,
        "condition": false,
      },
      {
        "tagId": 1,
        "condition": false,
      }
    ],
    "ruleSetId": 3,
  }
]
}
}

To understand JSON remember basic rule: read JSON backward. It means:

  • property
  • object.property
  • arrayOfObfects['id'].object.property
  • mainObject.arrayOfObfects['id'].object.property

and so on. Good luck!

Comments