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:
https://api.json-content-importer.com/extra/json/n1.json
and
https://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”

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

twig:

{% set secondjsonurl = "https://api.json-content-importer.com/extra/json/n2.json" %}
{% set sc = "[jsoncontentimporterpro url="~secondjsonurl ~"]{{_context | json_encode}}[/jsoncontentimporterpro]" %}
{% 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:


Warning: Undefined array key 2 in /www/htdocs/w01dd09b/api.json-content-importer.com/wp-content/plugins/jsoncontentimporterpro3/class-json-content-importer-pro.php on line 1013
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