Liquid transform - how to retain JSON object literal unchanged - json

I have JSON that looks like this:
[
{
"Data": {
"BagData": null,
"OtherData": {
"Responses": [
"test"
]
}
}
},
{
"Data": {
"BagData": {
"BagWeight": 20.0,
"ExceededBy": 0.0
},
"OtherData": null
}
}
]
I'm trying to transform this using a Liquid template. I want to simply output the JSON object literal for "Data" unchanged, but using a different property name so that the output is:
[
{
"MessageDetails": {
"BagData": null,
"OtherData": {
"Responses": [
"test"
]
}
}
},
{
"MessageDetails": {
"BagData": {
"BagWeight": 20.0,
"ExceededBy": 0.0
},
"OtherData": null
}
}
]
I'm using the following Liquid transform template, however it's outputting nothing in place of transaction.Data (content definitely contains the correct array of JSON object literals)
[
{% for transaction in content %}
{
"MessageDetails": {{ transaction.Data }},
},
{% endfor %}
]
How do I output the value of "Data" unchanged? I want it to work even if the structure for the value of "Data" changes. So, if the value of "Data" is:
{
"SomethingCompletelyDifferent": null
}
I still want that to appear in the output, so that the final output would be:
[
{
"MessageDetails" : {
"SomethingCompletelyDifferent": null
}
},
...
]

Have you tried breaking the JSON down further so it is more specific?
{% for transaction in content %}
{
"MessageDetails": {
"BagData": {
"BagWeight": {{transaction.MessageDetails.BagData.BagWeight}},
"ExceededBy": {{transaction.MessageDetails.BagData.ExceededBy}}
},
"OtherData": {{transaction.MessageDetails.OtherData}}
}
},
{% endfor %}
When writing out the template I find that you need to be specific on how you want it to look. Writing out each key and then calling its value allows you to template the JSON exactly how you want it too.
The for loop maps through each object it has received and will create what you have templated per object.

Related

extract subset of imported json file in vue's data() function

I have imported the following json file:
[
{
"case_id": "1234",
"thread": [
{
"t_id": "1111",
"text": "test"
},
{
"t_id": "2222",
"text": "test"
}
]
},
{
"case_id": "5678",
"thread": [
{
"t_id": "9999",
"text": "test"
},
{
"t_id": "8888",
"text": "test"
},
{
"t_id": "777",
"text": "test"
}
]
}
]
using the following:
import cases from '../cases.json'
The whole json dataset is available in cases variable and can be used in the template with the support of v-if and v-for.
How can I create a separate dataset (thecase) that contains only threads for a given case_id? In the template I would only like to use v-for to display all threads for a given case_id.
Below is my export default section:
export default {
name: "details",
props: {
case_id: {
required: true,
type: String
}
},
data () {
return {
cases,
thecase: ### THIS IS THE PART I CANNOT FIGURE OUT ###
}
}
};
You can remove thecase from data options and use a computed property instead for thecase. Inside the computed property, we will need to use array .find() method to find the case where case_id is same as the case_id passed in the prop:
data: {
cases,
},
computed: {
thecase: function() {
return this.cases.find(c => c.case_id === (this.case_id || ''))
}
}
and then you can use v-for on thecase.thread just like you would do for a data option like:
<li v-for="item in thecase.thread" :key="item.t_id">
{{ item.text }}
</li>
You can further modify it and use v-if & v-else to show a text like No cases were found with give case id in case there is no match found.

Parsing JSON to JSON in Liquid Template using for loop. How to iterate through lists in JSON using Liquid Template?

{
"SAP": [
{% for Record in content %}{
{% assign res = Record.d['results'] %}
"SSO ID": "{{ res[0].username }}"
},
{% endfor %}
]
}
Using this - i am only able to get username field of the first element from both results but not the second element. I want to be able to iterate through all the elements both results and get their values..
PLEASE HELP!!
For our requirement, I initialize a variable named "data" and store the below value to simulate your situation.
{
"content": [
{
"d": {
"results": [
{
"username": "hury11",
"email": "test#mail.com"
},
{
"username": "hury22",
"email": "test#mail.com"
},
{
"username": "hury33",
"email": "test#mail.com"
}
],
"_count": "17425",
"_next": ""
}
},
{
"d": {
"results": [
{
"username": "hury44",
"email": "test#mail.com"
},
{
"username": "hury55",
"email": "test#mail.com"
},
{
"username": "hury66",
"email": "test#mail.com"
}
],
"_count": "17425",
"_next": ""
}
}
]
}
Then parse json and use "Transform JSON to JSON" action.
My liquid template shown as below:
{
"SAP": [
{% for Record in content %}
[
{% for result in Record.d.results %}
{
"SSO ID": "{{result.username}}",
"email": "{{result.email}}"
},
{% endfor %}
],
{% endfor %}
]
}
If you want the d map in your result json data, the liquid template should be as below:
{
"SAP": [
{% for Record in content %}
{"d":
[
{% for result in Record.d.results %}
{
"SSO ID": "{{result.username}}",
"email": "{{result.email}}"
},
{% endfor %}
]
},
{% endfor %}
]
}
Hope it helps~

JSON liquid mapping using payload

I want to access the the below JSON payload :
[
{
"ApplicantContactDetail": [
{
"ApplicantBaseDetails": {
"CATEGORY": "P",
"ULN": "1265847478"
} }
]
}
]
and need output as
{
"Number":2,
"Data":
{
"ResidencyCode":"CATEGORY"
}
}
i need to map CATEGORY in the output from liquid template, may i know the correct syntax for this to achieve.

View doesn't load JSON

I'm working with Symfony 3, Angular JS and Twig. My problem comes when I try to generate a JSON in a view(html.twig).
My Model (MongoDB)
{
"_id" : ObjectId("5a1feb783e06fa060c0076b2"),
"contenido" : [
[
{
"type": "item",
"name": "Foo",
"id": 5
},
{
"type": "item",
"name": "Bar",
"id": 6
}
]
]
}
My Controller:
class Controlador extends Controller
{
// This method retrieve data (Document) from MongoDB
public function renderizar(Request $request)
{
...
$repository= $this->get('doctrine_mongodb')
->getManager()
->getRepository('AppBundle:Class');
$object = $repository->find('5a1feb783e06dfa060c0076b2');
$contenido = json_encode($object->getContenido());
$contenidoB = htmlentities($contenido);
$contenidoC = html_entity_decode($contenidoB);
$contenidoDef = json_decode($contenidoC);
return $this->render('view.html.twig', 'contenido' => $contenidoDef));
}
}
I want to generate that JSON in the view (inside the AngularJS's controller) in order to render a drag and drop panel from AngularJS.
My view
{% block body %}
<!doctype html>
<html >
<head>
<script>
angular.module("app").controller("NestedListsDemoController", function($scope)
{
$scope.models = {
dropzones: {
"A": [
{
"type": "container",
"name": "container",
"id": 4,
"columns": [
[
{
"type": "item",
"name": "Foo",
"id": 5
},
{
"type": "item",
"name": "Bar",
"id": 6
}
]
]
}
]
}
};
});
</script>
...
{% endblock %}
The Angular's controller doenst recognize the JSON in view if I use variable that contains that JSON.
dropzones: {
...
"A": {{ contenido|raw }}
}
or
dropzones: {
...
"A": {{ contenido }}
}
However, If I write the JSON in the view, it works. But I need to put the content from that variable. Any idea? Could anyone help me?
I solved the problem changing the string I would received in controller. When I retreive the document from MongoDB (I don't know why) comes with an extra double [ ] . Then, the drag and drop panel didn't recognize that JSON format...
To solve, first I removed them in controller, secondly I generated the JSON in the view as follows:
dropzones: {
"A": [ {{ contenido|raw }} ]
}
That's all, it works for me.
Use this:
dropzones = {{ contenido|json_encode|raw }};

Jekyll and Json files for data - How to retrieve the right data in a general way?

Suppose to have a json with this format:
{
"components":[
{
"id": "c3-1",
"type":"charts",
"component":"ChartC3Line",
"description":"Chart with round",
"picture": "/assets/img/components/chartc3-round.png",
"project": "yyy",
"date": "06-07-2015",
"dateupdate": "08-07-2015",
"status":" production"
},
{
"id": "c3-2",
"type":"charts",
"component":"ChartC3Date",
"description":"Chart with Date",
"picture": "/assets/img/components/chartc3-withdate.png
id: c3-2",
"date": "06-07-2015",
"project": "xxx",
"dateupdate": "08-07-2015",
"status":" production"
}]
}
I'd like to be able to retrieve informations about the second object with id: c3-2.
Premise:
I don't want to get it by index;
How can I get it by value ? by id for example.
Surely I have to change the structure of my json file.
How will you change it ?
For now I'm able to get the right value by index:
{% assign comp = site.data.components.components[1] %}
{{ comp.component }}
You can structure your _data/components.json like this :
{
"c3-1" : {
"type":"charts",
"component":"ChartC3Line",
"description":"Chart with round"
},
"c3-2" : {
"type":"charts",
"component":"ChartC3Date",
"description":"Chart with Date"
}
}
You can now get an element by his id : {{ site.data.components["c3-1"] }}.