Shared array for nested json

How will I be able to fetch the content URL from stage envt from the below json using shared array:
I was using below code for json files without nesting:

var TestEnvtURL = new SharedArray("Test Envt Links", function () {
 return JSON.parse(open('./data/dataURL.json')).TestEnvt; 
});

@imiric @mstoykov how can I handle nesting in the same line of code as above.

{
    "TestEnvt": [
        {
            "content_url": [
                "/p/20009789466",
                "/p/20009789466",
                "/p/20009789566",
            ],
            "images_url": [
                "mx/20009789466",
                "mx/20009789466",
                "mx/20009789566",
            ]
        }
    ],
    "StageEnvt": [
        {
            "content_url": [
                "/p/20009789455",
                "/p/20009789455",
                "/p/20009789555",
            ],
            "images_url": [
                "mx/20009789455",
                "mx/20009789455",
                "mx/20009789555",
            ]
        }
    ],
     "ProdEnvt": [
        {
            "content_url": [
                "/p/20009789422",
                "/p/20009789422",
                "/p/20009789522",
            ],
            "images_url": [
                "mx/20009789422",
                "mx/20009789422",
                "mx/20009789522",
            ]
        }
    ]
}

Hi @divinp

Is there something wrong with doing

var StageEnvtURL = new SharedArray("Stage Envt Links", function () {
 return JSON.parse(open('./data/dataURL.json')).StageEnvt; 
});
var ProdEnvtURL = new SharedArray("Prod Envt Links", function () {
 return JSON.parse(open('./data/dataURL.json')).ProdEnvt; 
});

Note: the names (the first argument to SharedArray) need to be different for different contents

This wont give us proper values as we are having nested json, and when we print in console it will be like :
Url Selected : [object Object]

as we have not specified from which json we have to get the data : like StageEnvt.content_url

can we achieve something like that?

I guess I misunderstood the question then the answer is that you sould probably do:

var TestEnvtContentURL = new SharedArray("Test Envt content Links", function () {
 return JSON.parse(open('./data/dataURL.json')).TestEnvt[0].content_url; 
});

var TestEnvtImagesURL = new SharedArray("Test Envt images Links", function () {
 return JSON.parse(open('./data/dataURL.json')).TestEnvt[0].images_url; 
});

/// later 
var contentURLIndex = generateContentURLIndex();
var contentURL = TestEvntContentURL[contentURLIndex % TestEnvtContentURL.length];

Where generateContentURLIndex is w/e you want, you can look at threads such as When parameterizing data, how do I not use the same data more than once in a test? or Want unique data per vus,and each loop sequentially in loop for some ideas on how to generate index.

Is that what you wanted?

1 Like

Thanks @mstoykov it worked :slight_smile: