简体   繁体   中英

Parsing and filtering JSON using Jekyll/Liquid

I try to select and use the sub-tree of my navigation, specified in JSON, in Jekyll.

This is the file _data/navigation.json :

    {
    "main": [
        {"text": "a", "children" : [
            {"text": "aa" },
            {"text": "ab" }
            ]},
        {"text": "b", "children": [
            {"text": "ba", "children": [
                {"text": "baa" },
                {"text": "bab" },
                {"text": "bac" }
                ] },
            {"text": "bb", "children": [
                {"text": "bba" },
                {"text": "bbb" },
                {"text": "bbc" }
                ]},
            {"text": "bc", "children": [
                {"text": "bca"},
                {"text": "bcb"}
                ]},
            {"text": "bd" },
            {"text": "be" }
            ]},
        {"text": "d" },
        {"text": "e"},
        {"text": "f" },
        {"text": "g" }
    ] 
}

Now I want to get the subtree of 'b':

{% assign stuff = site.data.navigation.main | where:"text","b"  %}

If I print this via {{ stuff }} , jekyll/liquids serves me an one line string representation starting {"text": "b", "children":... , as one could expect. {{stuff | size}} {{stuff | size}} is 1, so I can expect, it is one object, instead a sequence of characters internally. But if I want to go on working with this structure, I get no output, stuff.text , stuff["text"] and so on does not work.

Has someone a hint for me?

If you use {{ stuff | inspect }} {{ stuff | inspect }} you can see that it is an array.

You can catch the object inside with {% assign stuff = site.data.navigation.main | where:"text","b" | first %} {% assign stuff = site.data.navigation.main | where:"text","b" | first %} {% assign stuff = site.data.navigation.main | where:"text","b" | first %} .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM