Best way to access JSON data?

Hi, what the best way to access JSON data? For example, let’s say I wanted to access info_01 from the Purple Original product in this case below. Do I need to put everything into arrays and access them by index (!?) or is a there a simpler way. For example something like text = Purple.products.Purple Original.Info_01

{
	"brands": [{
		"Purple": [{
			"brand_logo": "some_logo.png",
			"products": [{
				"Purple Original": [{
					"tagline_01": "The Original Purple Mattress",
					"info_01": "Some info about the purple original mattress"
				}],
				"Purple 2": [{
					"tagline_01": "The Second Purple Mattress",
					"info_01": "Some info about the purple 2 mattress"
				}]
			}]
		}],
		"Other Brand": [{}],
		"Another Brand": [{}]
	}]
}

Ηι @Grimmy,

I would say don’t use arrays unless you have a specific reason to do so. For example arrays are used for properties that list several objects of the same type using indices (0,1,2,3 etc).

In your case I’d simplify your JSON like this, if your brands are always identified by string based ids (e.g. Purple, Purple Original etc):

{
  "brands": {
    "Purple": {
      "brand_logo": "some_logo.png",
      "products": {
        "Purple Original": [
          {
            "tagline_01": "The Original Purple Mattress",
            "info_01": "Some info about the purple original mattress"
          }
        ],
        "Purple 2": [
          {
            "tagline_01": "The Second Purple Mattress",
            "info_01": "Some info about the purple 2 mattress"
          }
        ]
      }
    },
    "Other Brand": {},
    "Another Brand": {}
  }
}

Now you can access your products using dot notation:

var purpleOriginalInfo = brands.Purple.products['Purple Original'][0].info_01;

The reason I used an array for Purple Original is to escape the space you have there, those aren’t allowed when using dot notation. And if you can do so without them it’s good to get rid of them.

Edit: Fixed the missing array getter in the dot notation code line.

1 Like

Perfect. Thank You!!

Hi, when I print the following within my script that defines the JSON data (appManager.js) it prints fine:

var mattressData = this.mattressData.resource;
console.log(mattressData.brands.Purple.products.Purple_Original.info_01);

However, when I try to access this data from another script I get an error (TypeError: Cannot read property ‘Purple’ of undefined) using the following code:

this.appManager=this.app.root.findByName("AppManager"); console.log("HELLO:"+this.appManager.script.appManager.mattressData.brands.Purple.products.Purple_Original.info_01);

Normally I can access variables from other scripts without problem but it seems to have an issue accessing the JSON. What am I doing wrong?

Thanks :slight_smile:

SOLVED: I had to do something like this in my original script so I can access it from other scripts…

var mattressData = this.mattressDataResource.resource;
this.mattressData=mattressData;
console.log(mattressData.brands.Purple.products.Purple_Original.info_01);
1 Like

Without seeing the code, shouldn’t this be:

this.appManager.script.appManager.mattressData.resource.brands.Purple.products.Purple_Original.info_01

As mattressData is a script attribute to a JSON asset?

Thanks.I tried that but that didn’t work. The issue seemed to do with the scope of the mattressData variable.

when I just do

var mattressData=42;

I dont seem to be able to access it from elsewhere…

If I do this

var mattressData
this.mattressData=42;

I can.

Is that correct?

I can access individual JSON nodes but when it comes to nodes with arrays my console just retunrs something like [object,object]. For example; I run the following command on the JSON to retrieve a list of the sizes available:

 for (var x=0; x<mattressData.brands.Purple.products.Purple_Original.Sizes.length; x++)
        {
            console.log("Hello"+mattressData.brands.Purple.products.Purple_Original.Sizes[x]);
        }

JSON:

{
	"brands": {
		"Purple": {
			"brand_logo": "some_logo.png",
			"products": {
				"Purple_Original": {
					"tagline_01": "The Original Purple Mattress",
					"info_01": "Some info about the purple original mattress",
					"info_02": "Some info about the purple original mattress",
					"info_03": "Some info about the purple original mattress",
					"Sizes": [{
							"Double": {"length":25, "width":15}
						},
						{
							"King": {"length":29, "width":19}
						},
                        {
							"Extra_Large": {"length":35, "width":25}
						}
					]

				}

			},
			"Purple_2": {
				"tagline_01": "The Second Purple Mattress",
				"info_01": "Some info about the purple 2 mattress"
			}

		},
		"Other_Brand": {},
		"Another_Brand": {}
	}
}

…this just returns
“Hello[object Object]”
“Hello[object Object]”
“Hello[object Object]”

I am trying for it to return:
HelloDouble
HelloKing
HelloExtra_Large

What am i doing wrong?
Thanks

You would need to go a bit deeper into the objects. Like: console.log("Hello"+mattressData.brands.Purple.products.Purple_Original.Sizes[x].Double.length);

You might want to consider reorganizing your objects since there is no standard field to call from when they’re presented in an array like this. It makes cycling trough everything harder. One option would be like this:

{
	"brands": {
		"Purple": {
			"brand_logo": "some_logo.png",
			"products": {
				"Purple_Original": {
					"tagline_01": "The Original Purple Mattress",
					"info_01": "Some info about the purple original mattress",
					"info_02": "Some info about the purple original mattress",
					"info_03": "Some info about the purple original mattress",
					"Sizes": [{
							"name" : "Double",
							"dims": {"length":25, "width":15}
						},
						{	
							"name" : "King",
							"dims": {"length":29, "width":19}
						},
                        {
                        	"name" : "Extra Large",
							"dims": {"length":35, "width":25}
						}
					]

				}

			},
			"Purple_2": {
				"tagline_01": "The Second Purple Mattress",
				"info_01": "Some info about the purple 2 mattress"
			}

		},
		"Other_Brand": {},
		"Another_Brand": {}
	}
}

That way you could do something like:

console.log("Hello"+mattressData.brands.Purple.products.Purple_Original.Sizes[x].name);

1 Like

Perfect. Thanks. With your modified JSON structure above to print all the sizs I can just do:

for (var x=0; x<mattressData.brands.Purple.products.Purple_Original.Sizes.length; x++)
        {
            console.log("Hello"+mattressData.brands.Purple.products.Purple_Original.Sizes[x].name);
        }