Cannot access JSON array stored in native Shopify metafields - shopify

I'm trying to access metafields containing an array that I want to loop over.
I've tried solutions online to no avail. Do you have any suggestions?
This is the JSON.
[
{
"id": 1,
"title": "Why do pets love CBD so much?",
"pub": "ScienceDirect",
"url": "https://www.efsa.europa.eu/en/news/cannabidiol-novel-food-evaluations-hold-pending-new-data"
},
{
"id": 2,
"title": "When will CBD take over the world",
"pub": "Anna Research Institute",
"url": "https://www.efsa.europa.eu/en/news/cannabidiol-novel-food-evaluations-hold-pending-new-data"
}
]
The metafield is article.metafields.custom.source_snippet
In my article template I'm trying to access it via {% assign sources = article.metafields.custom.source_snippet %}
This usually worked with the old "Metafields Guru".
Thanks.

In shopify there are actually two types of JSON metafields: json and json_string.
json_string is the old and deprecated type and json is the new version.
If your metafield was a json_string your code would have worked, but with json to access the actual value you need to add .value so your code should look like this
{% assign sources = article.metafields.custom.source_snippet.value %}

Related

Zabbix pre-processing item get item within an array with javascript

I don't have any experience with Javascript so I'm reaching out for the community to accomplish the below.
I have a few API calls configured on Zabbix which are working just fine. The thing is, not all of the results within an item is importante for me so I need to grab just what I want out of the array.
I'm using the pre-processing option on Zabbix to grab what I'm after but I can't get the code correct.
For instance, the below is one of the results Zabbix is getting.
[
{
"batteryLife": "15 minutes",
"communityString": "public",
"instanceId": "260596.1",
"instanceName": "UPS-01",
"ipAddress": "10.1.100.44",
"modelNumber": "GXT4-10000RT230",
"name": "UPS-01",
"objectType": "ScUps",
"scName": "pth-pf-04",
"scSerialNumber": 260596,
"serialNumber": "unknown",
"status": "Up",
"statusDescription": "Online",
"type": "Liebert"
}
]
How Can I Use the pre-processing to grab just the "ipAddress" value for instance?
Thanks for the help.
PeteF
You can avoid JavaScript preprocessing where you can use JSONPath preprocessing, see https://www.zabbix.com/documentation/current/manual/config/items/preprocessing/jsonpath_functionality
In your case:
$[0].ipAddress
A useful tool for JSONPath is http://jsonpath.com/
In case someone have the same doubt as I was having here is how I got it working to return the "ipAdress" value
var json = JSON.parse(value);
return json.ipAddress;
In case there are more than one dictionary inside of an array.
var json = JSON.parse(value);
return json[0].ipAddress;

How to get Collection object in Shopify when selected from customize window

I have created a section where I have written in schema to select a collection. I can view this option in customize window of shopify. Now on code part of this section I want the whole collection object so I can get image, title, description etc. from that collection.
{% schema %}
{
"name": "Featured Collection",
"settings": [
{
"type": "collection",
"id": "featured_collection",
"label": "Collection"
}
}
{% endschema %}
How I am trying to get the collection object
{% assign collection = section.settings.featured_collection %}
But this returns only the collection handle. Please tell me how could I get collection title, image & description
You have the collection handle. collections is a global Liquid object. Therefore you can reference any specific collection using the handle. Why can you not just do this?
{% assign x = collections['my-handle'] %}
And then use the title, image and description assigned to x?

Can any one elaborate Shopify default _st js object?

When I execute console.log(__st);
in any Shopify site, it returns JSON
{
"a": 22971355,
"offset": -14400,
"reqid": "aa1c5fad-1b95-43c2-aa29-c6d053e5c6d0",
"pageurl": "demo-new-store-1.myshopify.com/",
"t": "prospect",
"u": "e50223bc3803",
"cid": 213975957513,
"p": "home"
}
What the meaning of all element and how used(help) it I know some of them
except a, t, u
__st It looks like this is an internal object for Shopify internal tools/app. It has internal data about the page/user etc.
I do not think this object is intended to be used by anyone outside of Shopify dev team.

How can I use REST url as data in Vega-lite

I have this REST API that returns tabular data in the following way:
{"data": [{"el1": 8, "el2": 9}, {"el1": 3, "el2": 4}]}
I would like to use el1 and el2 in a Vega-lite chart. How should I refer to the elements in the array?
From the documentation here:
(JSON only) The JSON property containing the desired data. This parameter can be used when the loaded JSON file may have surrounding structure or meta-data. For example
"property": "values.features"
is equivalent to retrieving json.values.features from the loaded JSON object.
It seems that you can try to specify the "property" property (punny, eh) on the format. Something like this:
"data": {
"url": <your url here>,
"format": {
"type": "json",
"property": "data"
},
}
Disclaimer: I haven't actually tested this but it looks to be supported (:

Shopify - Get product title in liquid?

In the theme settings the end user is able to select a product from a drop down list of all products. This is done in the settings_scheme.json file.
[
{
{
"name": "Home page - Feature Box 2 (Product 1)",
"settings": [
{
"type": "product",
"id": "homepage-grid-product-1"
}
]
}
]
Then using liquid I want to display the name of the product.
On my page I am using the liquid code:
{{ settings.homepage-grid-product-1 }}
This seems to display the SEO url of the product, how can I make the product title display?
I'm sure this is really straightforward, I am still learning and not quite sure what to look up so not been able to find an answer online.
Cheers, DB.
You can always do a handle lookup via the all_products object.
all_products[settings.my-product-handle]