Django loop through json object - django

How to loop through a JSON object in Django template?
JSON:
"data": {
"node-A": {
"test1A": "val1A",
"test2A": "val2A",
"progress": {
"conf": "conf123A"
"loc": "loc123A"
},
"test3A": "val3A"
},
"node-B": {
"test1B": "val1B",
"test2B": "val2B",
"progress": {
"conf": "conf123B"
"loc": "loc123B"
},
"test3B": "val3B"
}
}
I am having trouble accessing the nested values "conf" and "lock" inside "progress". How can I access them in Django template if the data is passed as context i.e. return (request, 'monitor.html', {"data_context": json_data['data']})?

they way you have it set up, your data is in a dictionary called 'data_context'. To access what you need in the template it would be {{data_context.test1A}}.
to not have to use 'data_context.' try this instead,
return (request, 'monitor.html', json_data['data'].to_dict())

Dictionary lookup, attribute lookup and list-index lookups are implemented with a dot notation:
{{ my_dict.key.key_nested }}

As the JSON format behaves like a dictionary in Python, the data stored with the specified keys conf and loc should be accessible with the python notation for dictionaries. Since the provided JSON can be seen as a nested dictionary, you need to "concat" the keys respectively to get your desired data.
Your return statement returns a dictionary which I will call ret so the structure should be:
{"data_context": {
"node-A": {
"test1": "val1A",
"test2": "val2A",
"progress": {
"conf": "conf123A",
"loc": "loc123A"
},
"test3": "val3A"
},
"node-B": {
"test1B": "val1B",
"test2B": "val2B",
"progress": {
"conf": "conf123B",
"loc": "loc123B"
},
"test3": "val3B"
}
}
}
Therefor to access conf and loc:
ret["data_context"]["node-A"]["progress"]["conf"]
will get you the value stored at conf in node-A

Related

how to obtain data argument to provide to mustache template in moodle

In moodle (4.0) I have the need to call
$this->output->render_from_template('core_courseformat/local/content/section/cmlist', $sectionData);
From within a renderer. The goal is to render the normal, native, cmlist component in a particular place on the page. But the way that I am currently getting the value of $section does not seem to work. My template renders nothing. I can see from the github source that this template expects data in this format:
Example context (json):
{
"cms": [
{
"cmitem": {
"cmformat": {
"cmname": "<a class=\"aalink\" href=\"#\"><span class=\"instancename\">Forum example</span></a>",
"hasname": "true"
},
"id": 3,
"module": "forum",
"extraclasses": "newmessages"
}
},
{
"cmitem": {
"cmformat": {
"cmname": "<a class=\"aalink\" href=\"#\"><span class=\"instancename\">Assign example</span></a>",
"hasname": "true"
},
"id": 4,
"module": "assign",
"extraclasses": ""
}
}
],
"hascms": true,
"showmovehere": true,
"movingstr": "Moving this activity: folder example",
"cancelcopyurl": "#",
"movetosectionurl": "#",
"strmovefull": "Move 'folder example' to this location"
}
}}
https://github.com/moodle/moodle/blob/1d99ba19a21d57e9f1ed4211a8eeee00e50b7baf/course/format/templates/local/content/section/cmlist.mustache
But here's the challenge. How do I get an object in that format with the data needed to feed the template so it can render the correct CM list items?
Currently I am tring:
$sectionData = get_fast_modinfo($course->id)->get_section_info($section);
But it doesn't seem to return the data structured in the right way.
Any help appreciated.
You can use the function export_for_template of cmlist render class.
Something like this:
$cmlist = new \core_courseformat\output\local\content\section($format, $section);
$data->cmlist = $cmlist->export_for_template($OUTPUT);
and then send the data to the template.
I recommend you imitate the behavior of a moodle as he performs here:
https://github.com/moodle/moodle/blob/7ce003b666a66b465ce9335f430a6e4d3535a7f1/course/format/classes/output/local/content/section.php#L223

What is the difference between getting no prefix key and getting a prefix key with empty string value in get_bucket_lifecycle API response?

I'm running a python program that uses boto3 get_bucket_lifecycle_configuration API to get lifecycles set on a given bucket. I'm getting the following response -
{"Rules":[
{
"Filter":{
"Prefix":""
},
"Status":"Enabled",
"ID":"deleteIncompleteMPU",
"AbortIncompleteMultipartUpload":{
"DaysAfterInitiation":1
}
},
{
"Filter":{
},
"Status":"Enabled",
"NoncurrentVersionExpiration":{
"NoncurrentDays":12
},
"Expiration":{
"Days":210
},
"AbortIncompleteMultipartUpload":{
"DaysAfterInitiation":1
},
"Transitions":[
{
"Days":30,
"StorageClass":"DEEP_ARCHIVE"
}
],
"ID":"all-curr-GDA-30-non-curr-delete-12"
}
],
"ResponseMetadata":{
}
}
}
My question is that for ID deleteIncompleteMPU, the prefix value is an empty string "Prefix":"" but for ID all-curr-GDA-30-non-curr-delete-12 there is no Prefix key under the Filter key "Filter":{ }.
Don't both things signify that the rule is set on the entire bucket? If no, then what is the difference?

Get keys from Json with regex Jmeter

I'm hustling with regex, and trying to get the id's from this body.
But only the id´s in the members list, and not the id in the verified key. :)
To clarify, I'm using Regular Expression Extractor in JMeter
{
"id": "9c40ffca-0f1a-4f93-b068-1f6332707d02", //<--not this
"me": {
"id": "38a2b866-c8a9-424f-a5d4-93b379f080ce", //<--not this
"isMe": true,
"user": {
"verified": {
"id": "257e30f4-d001-47b3-9e7f-5772e591970b" //<--not this
}
}
},
"members": [
{
"id": "88a2b866-c8a9-424f-a5d4-93b379f780ce", //<--this
"isMe": true,
"user": {
"verified": {
"id": "223e30f4-d001-47b3-9e7f-5772e781970b" //<--not this
}
}
},
{
"id": "53cdc218-4784-4e55-a784-72e6a3ffa9bc", //<--this
"isMe": false,
"user": {
"unverified": {
"verification": "XYZ"
}
}
}
]
}
at the moment I have this regex :
("id": )("[\w-]+")
But as you can see here it returns every guid
Any ideas on how to go on?
Thanks in advance.
Since the input data type is JSON, it is recommended to use the JMeter's JSON Path Extractor Plugin.
Once you add it, use the
$.members[*].id
JSON path expression to match all id values of each members in the document that are the top nodes.
If you may have nested memebers, you may get them all using
$..members[*].id
You may test these expressions at https://jsonpath.com/, see a test:

How to iterate over a json object in Python?

I have a json object contaning currencies as listed below which I need to convert it into my model and save it into the DB. Also is there a way to save the list of models in one go?
{
"results": {
"ALL": {
"currencyName": "Albanian Lek",
"currencySymbol": "Lek",
"id": "ALL"
},
"KWD": {
"currencyName": "Kuwaiti Dinar",
"id": "KWD"
},
"LSL": {
"currencyName": "Lesotho Loti",
"id": "LSL"
},
"MYR": {
"currencyName": "Malaysian Ringgit",
"currencySymbol": "RM",
"id": "MYR"
},
"MUR": {
"currencyName": "Mauritian Rupee",
"currencySymbol": "₨",
"id": "MUR"
}
}
}
I tried this :
for key,value in currencies.results :
#print(currency)
#print(value)
However, I get the following error :
"Too many attribures to unpack, expected 2
Can someone help me with this?
I think it should be like this:
results = currencies.get('results')
for key, value in results.items(): # for python3
print(key, value)
for key, value in results.iteritems(): # python2.7
print(key, value)
You should iterate as
for result in results:
for currency in result:
print(result)

Accessing list of dictionaries in template by index

I have a list of dictionaries in the django template. I wish to use its values in a form.
[
{
"movie_id": 1950,
"title": "In the Heat of the Night (1967)"
},
{
"movie_id": 3741,
"title": "Badlands (1973)"
},
{
"movie_id": 3959,
"title": "Time Machine, The (1960)"
},
{
"movie_id": 4101,
"title": "Dogs in Space (1987)"
},
{
"movie_id": 8572,
"title": "Littlest Rebel, The (1935)"
},
{
"movie_id": 65230,
"title": "Marley & Me (2008)"
},
{
"movie_id": 105954,
"title": "All Is Lost (2013)"
}
]
In my template, I want to get values of each field by index. ie. for the
list[0]['movieId'], I would like to get the value 1950. Is that possible?
You can access it via dot (.) notation.
From the documentation
Technically, when the template system encounters a dot, it tries the following lookups, in this order:
Dictionary lookup
Attribute or method lookup
Numeric index lookup
So you can use list.0.movieId. The 0 dot attribute will access the list element and movieId will do the dictionary lookup.