Iterating over json object programmatically with Handlebars - json

I have some json that looks like this:
{{...},
"skills": [{
"languages": ["Java", "JavaScript", "C", "HTML5", "CSS3", "PHP", "Python"],
"j2ee": ["JSP", "Struts", "Tiles"],
"servers": ["Apache Tomcat 5, 6", "Webshpere Application Server", "Node.js", "Django (Python framework)"],
"tools": ["Eclipse and Rational Application Developer", "Maven", "vim", "*nix CLI"],
"versions": ["Git", "MKS", "Accurev", "SubVersion", "CVS"]
}],
{...}}
and some handlebars in a partial that looks like this:
{{#each skills}}
<div id="{{this}}">
<ul>
{{#list this}}
<li>{{this}}</li>
{{/list}}
</ul>
</div>
{{/each}}
This obviously does not work, but my thought process was that I could iterate over the skills array programmatically so that I wouldn't have to put the keys in, and then iterate over each key's value (an array).
I know I could just do something like
{"skills":
[{"name": "languages",
"values":["Java",...]},
{"name": "j2ee",
"values":["JSP",...]},
{...}]}
but I'm wondering if there's a way to do it with the first structure.

Related

How to parse, and render, json for use in Django template

I'm a newbie in Django and trying to learn, but I'm confused about how I can render data pulled from URL in a template in Django and display it in the HTML page.
The json data sample is:
{
"docs":
[
{
"hostIP": "X.X.X.X",
"time": "August 13, 2018 13:43:44",
"site":
[
{
"site": "site1",
"path": "/path/to/site1",
"git_branch": "master",
"git_commit_message": "New changes"
},
{
"site": "site2",
"path": "/path/to/site2",
"git_branch": "master",
"git_commit_message": "add card"
}
]
}
]
}
Also how i can loop it using Jinja2? Please someone help me out of this.
In your view code, parse it with json.loads():
import json
data = json.loads(my_json_data)
Then pass in data as a context variable to the view. Then you can see these variables and loop over them how you want in the template.

Deserialize Json with object references

Is there a way to deserialize a JSON that includes references to objects that already exist inside it using typescript?
For example we have a grand parent "Papa" that is associated with two parents "Dad" and "Mom" that they have together two children, the json looks like:
{
"id_": 1,
"name": "Papa",
"parents": [
{
"#class": "com.doubleip.spot.mgmt.test.domain.model.Parent",
"id_": 1,
"name": "Dad",
"children": [
{
"#class": "com.doubleip.spot.mgmt.test.domain.model.Child",
"id_": 1,
"name": "Bob"
},
{
"#class": "com.doubleip.spot.mgmt.test.domain.model.Child",
"id_": 2,
"name": "Trudy"
}
]
},
{
"#class": "com.doubleip.spot.mgmt.test.domain.model.Parent",
"id_": 2,
"name": "Mom",
"children": [
1,
2
]
}
]
}
You may see that the children of Mom are just inserted as the value of their "id_" field. This happens due to JsonIdentityInfo used in Java and fasterxml library.
So we face problem in front-end deserialisation where we use typescript angular and primeng in order to visualise our data.
So we face problem in front-end deserialisation
you need to write most of the code yourself (or generate it using more code from your Java code).
That said, there are a few hydration helpers. I recommend : https://github.com/mobxjs/serializr

How can I access a sub-object in JsonDataObjects

Say I have the following JSON object, how can I access, directly (without iterating over the entire array of items), a subarray item of it, using Delphi and JsonDataObjects
{"menu": {
"header": "SVG Viewer",
"items": [
{"id": "Open"},
{"id": "OpenNew", "label": "Open New"},
null,
{"id": "Help"},
{"id": "About", "label": "About Adobe CVG Viewer..."}
]
}}
What is the Delphi code to directly access the subarray with id = "About" and read the label property?

Binding embedded Json in ember

How does bind values of embedded JSON data to input elements in EmberJS as I cannot seem to find a straight forward way to get that done.
update
It doesn't seem to work for a JSON object with this structure
{
"users": [
{
"_id": "534550428047526419000002",
"Name": "admin",
"Code": "admin",
"Age": 12,
"Sex": "Male",
"Ethnicity": "admin",
"EventTime": "",
"Accident": [
{
"value": true,
"risk": "Medium"
}
]
}
]
}
In the template you use the property name
{{foo}}
{{input value=foo}}
Example
http://emberjs.jsbin.com/wufegaca/1/edit
nested properties are handled the same way they are in most languages with the dot operator
{{foo.bar}}
{{input value=foo.bar}}
Example
http://emberjs.jsbin.com/wufegaca/2/edit
Using an array you need to iterate over the array. If you didn't how would Ember know which item you were referring to? Additionally upper case property names are problematic in handlebars, since it usually denotes a global namespace, so be wary if things don't appear to be working.
Example
http://emberjs.jsbin.com/wufegaca/3/edit

JSON format with gzip compression

My current project sends a lot of data to the browser in JSON via ajax requests.
I've been trying to decide which format I should use. The two I have in mind are
[
"colname1" : "content",
"colname2" : "content",
],
[
"colname1" : "content",
"colname2" : "content",
],
...
and
{
"columns": [
"column name 1",
"column name 2",
],
"rows": [
[
"content",
"content"
],
[
"content",
"content"
]
...
]
}
The first method is better because it is easier to work with. I just have to convert to an object once received. The second will need some post processing to convert it into a format more like the first so it is easier to work with in JavaScript.
The second is better because it is less verbose and therefore takes up less bandwidth and downloads more quickly. Before compression it is usually between 0.75% and 0.85% of the size of the first format.
GZip compression complicates things further. Making the difference in file size nearer 0.85% to 0.95%
Which format should I go with and why?
I'd suggest using RJSON:
RJSON (Recursive JSON) converts any JSON data collection into more compact recursive form. Compressed data is still JSON and can be parsed with JSON.parse. RJSON can compress not only homogeneous collections, but any data sets with free structure.
Example:
JSON:
{
"id": 7,
"tags": ["programming", "javascript"],
"users": [
{"first": "Homer", "last": "Simpson"},
{"first": "Hank", "last": "Hill"},
{"first": "Peter", "last": "Griffin"}
],
"books": [
{"title": "JavaScript", "author": "Flanagan", "year": 2006},
{"title": "Cascading Style Sheets", "author": "Meyer", "year": 2004}
]
}
RJSON:
{
"id": 7,
"tags": ["programming", "javascript"],
"users": [
{"first": "Homer", "last": "Simpson"},
[2, "Hank", "Hill", "Peter", "Griffin"]
],
"books": [
{"title": "JavaScript", "author": "Flanagan", "year": 2006},
[3, "Cascading Style Sheets", "Meyer", 2004]
]
}
Shouldn't the second bit of example 1 be "rowname1"..etc.? I don't really get example 2 so I guess I would aim you towards 1. There is much to be said for having data immediately workable without pre-processing it first. Justification: I once spend too long optimizing array system that turned out to work perfectly but its hell to update it now.