Community

How to get the value of the array from the tv field?

I have a large json file that I import via catalogfill as msProduct.
I want each array value within the tv “BigImages” to be inserted into a separate img tag.
Like this:
img src="[[*BigImages]] [0]"-(first value)
img src="[[*BigImages]] [1]"-(second value)

Please tell me how to do this correctly

1 Like

I don’t think I’d use a TV for this since you can get the JSON directly from the file and set a unique placeholder for each value based on the keys in the JSON – something like this (untested):

$data = file_get_contents('file/path');
$dataArray = $modx->fromJSON($data);
$modx->setPlaceholders($dataArray);
return;
1 Like

JSON file contains all information about all products (over 200 000) and that’s why I want to use tv fields =(

1 Like

200 000+ lines and each one contains an array with the necessary data

1 Like

Or maybe I should write all the data in 1 tv field (the whole line with json data) and from there use javascript or php?

1 Like

Hmm. That’s not a bad idea :slight_smile:

1 Like

TVs are not favored when they will be holding lots of data, as that is harder on the server. If your putting a lot of data into one or more tvs, or have a lot of tvs, it could affect performance. It also might not be the most organized method

With Modx just import the whole JSON and then manipulate the data, just import it into your db, and output the parts you want.

Maybe you could tell us more about what you want to do, your real goal here. Getting the data from JSON normally is pretty easy in Modx

I’m new to modx. I chose it because I needed to make a large online shop (200 000 + products) so that the site wouldn’t lag(with about 15.000 - 25.000 users). Except msImportExport I found only Catalogfill way to import. Server have 3 gb ram and 4 cores(turbo a2hosting) + infinite ssd gb
I have a large json file with all the products and I need to import them into the modx…
The whole store is set on minishop2. I have already installed mSearch2 and other components (except msImportExport since I already had Catalogfill)
If the best method is to import the json file into the database, can you tell me how to correctly get this data from there and put it in site?
The json string pattern looks like:
{“Big Images”: [“imgs/image1.jpg”, “imgs/image2.jpg”, “imgs/image3.jpg”], “Small Images”: [also some imgs], “Title”: [“title”], “Price”: [“N.nn”], “Color”: [“some color”], “Main Info”: [“html code with text there”]]},

1 Like

Ok good to provide more info, we will see some feedback later.

I do think you are right, you don’t want to import all of it, but this is outside my range.

Hmm maybe you are right about the tvs, if they are all small bits of info each it might be ok…

It seems like maybe you could output every page with a snippet which goes to the json and outputs the values required and outputs them into a chunk tpl, so when the user wants item 123 the snippet goes to the json and gets all id=123 data, then outputs as something like:

[[+title]]
[[+code]]

etc. That’s quite a lot of requests, but each seems smallish so maybe it can be manageable.

Let’s see what some of our devs have to say.

Standard MODX TVs don’t really have JSON values; typically they only have a “flat” string value. For more complex data structures, you’d typically see custom TV types that store JSON and have their own render snippet of sorts (e.g. Image+).

I think MiniShop2 has some product media management built in (or is that msGallery?). I’d imagine there’s a way to import your images into that, and that MS2 has a way to render those images. That unfortunately is mostly assumptions as I have very little personal experience with MS2.

1 Like