PRO: Calc with data from 2 JSON-URLs by merging the JSON-feeds

Challenge:
You have two JSON feeds you want to merge and calc with the JSON-values.


Example-Solution:
Example JSON-Feeds:
http://api.json-content-importer.com/extra/json/n1.json
and
http://api.json-content-importer.com/extra/json/n2.json


Create a new JCI-Template, name it e. .g “2json” and use one JSON-feed-URL as “URL of Template”

http://api.json-content-importer.com/extra/json/n1.json

twig:

{% set secondjsonurl = "http://api.json-content-importer.com/extra/json/n2.json" %}
{% set sc = "Fetching URL failed: A valid URL was not provided." %}
{% set json2feed = (sc | doshortcode ) %}
{% set json2feedk = (json2feed| json_decode ) %}
no2 from the 1st JSON-feed: {{ no2 }}<br>
this is the data from the 2nd JSON-feed: {{json2feedk | json_encode}}
<br>
now calc no1 * no2:
{{ json2feedk.no1 * no2 }}

Shortcode:

[jsoncontentimporterpro name=2json orderofshortcodeeval=10]

Explanation:

  • “sc” is a string containing a JCI-shortcode for retrieving the 2nd JSON-URL. This Shortcode could also one with just [jsoncontentimporterpro id=...] for using another JCI-Template (in case the request is more complex).
  • The “sc”-shortcode gives back {{_context | json_encode}}: This means the whole JSON is returned as pure string. Hence json2feed=(sc|doshortcode) executes the shortcode and sets json2feed as a JSON-string
  • json2feedk=(json2feed|json_decode) converts the JSON-string to an JSON-array, which can be used just like the root-JSON-array of the 1st JSON-feed: json2feedk.no1 * no2

Importaint settings:

  • Add “orderofshortcodeeval=10” to the shortcode! Otherwise the Shortcode in the Source might be executed at the wrong place

In action:
no2 from the 1st JSON-feed: 5
this is the data from the 2nd JSON-feed: {"no1":"8","unused":5433}
now calc no1 * no2: 40