I am trying to build up a json from a tree structure. I use django-mptt to build the tree. But impossible for me to create a JSON out of it.
I want the json to look like this:
[
{"name": "Parent 1",
"child": [
{"name": "Child 1-1"},
{"name": "Child 1-2"},
{"name": "Child 1-3"}
]},
{"name": "Parent 2",
"child": [
{"name": "Child 2-1"},
{"name": "Child 2-2"},
{"name": "Child 2-3"
,"child": [
{"name": "Child 2-3-1"},
{"name": "Child 2-3-2"}
]
}
]
}
];
It can have multiple/unlimited children.
So far I tried this. But unable to get the right syntax for the JSON
var json = [
{% recursetree nodes %}
{"text": "{{ node.item_title }}"
{% if not node.is_leaf_node %}
,"nodes": [
{{ children }}
{% endif %}
},
{% endrecursetree %}
];
This gets me this output:
var json = [
{"name": "Parent 1"
,"child": [
{"name": "Child 1-1"
},
{"name": "Child 1-2"
},
{"name": "Child 1-3"
},
},
{"name": "Parent 2"
,"child": [
{"name": "Child 2-1"
},
{"name": "Child 2-2"
},
{"name": "Child 2-3"
,"child": [
{"name": "Child 2-3-1"
},
{"name": "Child 2-3-2"
},
},
];
I cant figure out how to get the parentesis and hooks at the right places.
I'm really stuck! Can somebody help me out? Thanks.
Related
I have the following json which are nested. I am looking for the better approach to read the data. Below is the sample json
[
{
"id": 1,
"name": "some text",
"selections": [
{
"id": 38383,
"items": [
{
"type": "view",
"children": [
{
"nodeId": "groups",
"children": [
{
"nodeId": "groups 1",
"children": [
{
"type": "group level item",
"name": "text1",
"leaf": true,
"info": [
"groups ",
"groups 1",
"groups 2"
]
},
{
"type": "group level item",
"name": "text2",
"leaf": true,
"info": [
"groups ",
"groups 1",
"groups 2"
]
}
]
}
]
}
]
}
]
}
]
},
{
"id": 2,
"name": "some text",
"selections": [
{
"id": 23232,
"items": [
{
"type": "view",
"children": [
{
"nodeId": "Hirearchy",
"children": [
{
"nodeId": "Hirearchy level 1",
"children": [
{
"nodeId": "Hirearchy level 2",
"children": [
{
"type": "charactreistic value",
"name": "some text 1",
"leaf": true,
"info": [
"Hirearchy",
"Hirearchy level 1",
"Hirearchy level 2",
"Hirearchy level 3"
]
},
{
"type": "characterisitic value",
"name": "some text 2",
"leaf": true,
"info": [
"Hirearchy",
"Hirearchy level 1",
"Hirearchy level 2",
"Hirearchy level 3"
]
}
]
}
]
}
]
}
]
}
]
}
]
},
{
"id": 3,
"name": "some text",
"selections": [
{
"id": 2444,
"items": [
{
"type": "view",
"children": [
{
"nodeId": "category",
"children": [
{
"type": "some value",
"name": "some text 1",
"leaf": true,
"info": [
"category",
"category level 1"
]
},
{
"type": "some value",
"name": "some text 2",
"leaf": true,
"info": [
"category",
"category level 1"
]
}
]
}
]
}
]
}
]
},
{
"id": 4,
"name": "some text",
"selections": [
{
"id": 2444,
"items": [
{
"type": "view",
"children": []
}
]
}
]
},
{
"id": 5,
"name": "some text",
"selections": [
{
"id": 2444,
"items": []
}
]
},
{
"id": 6,
"name": "some text",
"selections": []
}
]
for id 1 - there is 3 levels of nesting and the last one has a property which says leaf = true. so in this i need to de structure the object to get the desire out put like
heading - group 2 (from info arrar)
name - ['text1', 'text2'] from the last leaf
for id 2 - there is 4 levels of nesting and the last one has a property which says leaf = true. so in this i need to de structure the object to get the desire out put like
heading - Hierarchy level 3
name - ['some text1', 'some text2']
for id 3 there is 2 levels of nesting and the last one has a property which says leaf = true. so i this
i need to se structure the object to get the desire output like this
heading - Category 1
name - ['some text 3', 'some text4']
for id 4 - there is 1 level of nesting but the array is blank
for id 5 - there is no nesting and the items array is blank
for id 6 - selections array is blank
Question 1. I need to write a generic method to loop thru the json object and find the node where the property 'leaf' is true.
Question 2. This generic method to work even if the nesting levels are 2 or 3 or 4 or it could be more also
Question 3. This generic method should not break the code when the array is empty also.
Requirement. this is required in angular 10 application where i need to destructure the object
Thanks in advance
For the last two weeks I've been taking a Shopify theme developer course and started developing my very own theme in Shopify. However, I have encountered a couple of strange problems. I currently have a header that contains a slideshow, where I can select images from the Shopify Customizer. However, I also need to display the main menu, and this is where the problem is. I have tried to add my menu in the schema like this:
{% schema %}
{
"name": "Main Menu",
"settings": [
{
"type": "link_list",
"id": "main_linklist",
"label": "Menu",
"default": "main-menu"
}
]
},
{
"name": "Homepage Carousel",
"settings": [
{
"type": "image_picker",
"id": "slide-1",
"label": "Slide 1"
},
{
"type": "image_picker",
"id": "slide-2",
"label": "Slide 2"
}
]
}
{% endschema %}
The error that I'm getting is 'Invalid JSON in tag 'schema'. I have also tried to make to schemas, but I quickly found out that this did not work. Can anyone point me in the right direction here? It has been driving me crazy for two days now, and I honestly can't find a fix for it.
You combining settings_schema.json and section {% schema %} logic, which is a "No no".
This is how a standard settings_schema.json should look like:
[
{
"name": "theme_info",
"theme_name": "Theme",
"theme_author": "N/A",
"theme_version": "1.0.0",
"theme_documentation_url": "https://help.shopify.com/manual/using-themes",
"theme_support_email": "theme-support#shopify.com"
},
{
"name": "General",
"settings": [
{
"type": "header",
"content": "Favicon"
},
{
"type": "image_picker",
"id": "favicon",
"label": "Favicon"
}
]
},
{
"name": "Social media",
"settings": [
{
"type": "header",
"content": "Social sharing image"
},
{
"type": "image_picker",
"id": "share_image",
"label": "Image"
}
]
},
{
"name": "Footer",
"settings": [
{
"type": "richtext",
"id": "copyright",
"label": "Copyright",
"info": "Use [year] to set the current year."
}
]
}
]
And this is how should a section {% schema %} look like.
{% schema %}
{
"name": "Slideshow",
"settings": [
{
"id": "header",
"type": "text",
"label": "Header",
"default": "Hello world"
},
{
"id": "content",
"type": "textarea",
"label": "Content"
}
],
"blocks": [
{
"type": "quote",
"name": "Quote",
"settings": [
{
"id": "content",
"type": "text",
"label": "Quote"
},
{
"id": "title",
"type": "text",
"label": "Title"
}
]
}
]
}
{% endschema %}
So to summarize you need to learn the difference between a settings_schema.json syntax and the section {% schema %} syntax.
At the moment I'm not sure if you are making your options inside the settings_schema.json or you are making a separate section. In both cases you need to fix the schema syntax.
If you are using the settings_schema.json there is no {% schema %} around the json.
If you are using a section {% schema %} you are allowed for only one object (if it's not a block):
{
"name": "Main Menu",
"settings": [
.....
]
}
Read the section docs: https://help.shopify.com/en/themes/development/sections
And the settings_schema docs: https://help.shopify.com/en/themes/development/theme-editor/settings-schema
I have the following db:
{
"a": [{
"name": "foo",
"thing": [{
"name": "bar",
"lyrics": ["1", "2", "3"]
}]
}, {
"name": "abc",
"thing": [{
"name": "123",
"list": ["one", "two"]
}]
}]
}
I can't seem to query it correctly. These two queries return the same thing, the entire db:
db.test.find({"a.name":"abc"})
db.test.find({"a.name":"foo"})
How do I find one collection instead of the whole db?
I would expect the first query to return:
{
"name": "abc",
"thing": [{
"name": "123",
"list": ["one", "two"]
}]
}
The two queries return the same document, because both queries match it.
This is one document
[{
"a": [{
"name": "foo",
"thing": [{
"name": "bar",
"lyrics": ["1", "2", "3"]
}]
}, {
"name": "abc",
"thing": [{
"name": "123",
"list": ["one", "two"]
}]
}]
}]
This is two documents
[{
"a": [{
"name": "foo",
"thing": [{
"name": "bar",
"lyrics": ["1", "2", "3"]
}]
}]
},
{
"a": [{
"name": "abc",
"thing": [{
"name": "123",
"list": ["one", "two"]
}]
}]
}]
You can get stats on of a collection like so
db.test.stats()
"count" will tell you how many documents are there.
Edit: To add to this, in your collection "test" a document has 1 field, which is "a" and is of type array that holds objects (documents). It has 2 array elements
First
{
"name": "foo",
"thing": [{
"name": "bar",
"lyrics": ["1", "2", "3"]
}]
}
Second
{
"name": "abc",
"thing": [{
"name": "123",
"list": ["one", "two"]
}]
}
Everything inside curly braces {..}, including the braces themselves, is a one single document, i.e. the whole your database contains only one document that you receive for any matching query. To receive the desired result, you have to re-write your JSON document as an array of documents inside square braces [..].
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}} 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 }} 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 %}.
I have a json object:
{
"context": [
{"name": "John", "node": [{"id": 1, "detail": "hello"}, {"id": 2, "detail": "world"}]},
{"name": "Andy", "node": [{"id": 3, "detail": "andy"}]},
{"name": "Dave", "node": [{"id": 4, "detail": "dave"}]},
]
}
and I want to get the list of detail of each person
[
["hello", "world"],
["andy"],
["dave"],
]
I am wondering if this is even possible? I have tried many things but the array got flatten out and that is not ideal.
The json you provide isn't valid (so I added a name of the array):
{
"persons": [
{"name": "John", "node": [{"id": 1, "detail": "hello"}, {"id": 2, "detail": "world"}]},
{"name": "Andy", "node": [{"id": 3, "detail": "andy"}]},
{"name": "Dave", "node": [{"id": 4, "detail": "dave"}]},
]
}
To get all details here:
$..detail
the result of it is:
[
"hello",
"world",
"andy",
"dave"
]
tested with http://jsonpath.curiousconcept.com/ (Jsonpath 0.8.3)
Okay, I'm guessing your JSON should be an array of objects and not an object containing an array. If so, then this is how you could do it;
$json = <<<EOF
[
{"name": "John", "node": [{"id": 1, "detail": "hello"},{"id": 2, "detail": "world"}]},
{"name": "Andy", "node": [{"id": 3, "detail": "andy"}]},
{"name": "Dave", "node": [{"id": 4, "detail": "dave"}]}
]
EOF;
$map = array_map(function($object) {
return array_map(function($detail) {
return $detail->detail;
}, $object->node);
}, json_decode($json));
This would then give you the values in the structure you want;
[
['hello', 'world'],
['andy'],
['dave']
]
The sample JSON has one excess comma. Assuming it has been removed, the following invocation:
jq -cf '[.context[] | [.node[] | .detail]]' input.json
produces:
[["hello","world"],["andy"],["dave"]]
http://stedolan.github.io/jq is very similar to JSONpath. See "jq for JSONpath users".