How to use intersect with json

Hello Community,

My code works fine when I use MD files. But when I try doing the code with JSON (and implementing modifications onto my code to use json), it fails. I am unable to figure out why.

Here is the sample content from the JSON (fcountries.json):

[
    {
        "FID": "26",
        "c_codes": "['au', 'fra', 'afg', 'ind', 'bra']"
    },
...
]

And here is the code that I have -

$fruitLocations:=  $.Site.Data.fcountries ".c_codes" "intersect" (slice .Params.fc_code)

The purpose of the above code here is - to find out the fruits which are available in a country. And what I am trying to do is search for the incoming fc_code (which is a country code) in the json and list out the IDs.

The above code does not print anything. However, if I create MD files for my json data, then, the following works fine as expected

{{ $fCountry := where $.Site.Pages "Section" "countries" }}
{{ $fCountry := where $fCountry "Params.c_codes" "intersect" (slice .Params.fc_code) }}

Would appreciate if someone here could tell me what I am doing wrong above.

Regards,
Sid.

There are two subsequent single quotes in your ‘c_codes‘.

Thanks for pointing that out. I changed and updated the codes on my file as well as on the original post, still no results.

Off-topic: Now I am wondering if that was an error why did the debugger not raise an issue.

You’ve quoted the entire array, so it’s a string, not an array.

Your JSON should look something like:

[
  {
    "FID": "26",
    "c_codes": ["au","fra","afg","ind","bra"]
  },
  {
    "FID": "27",
    "c_codes": ["au","fra"]
  }
]

1 Like