Need help for Logic in json liquid template

vijaykumarsivaj
New Member
2 0 0

Hi,

I am new to this liquid templates. I am having four elements in an json array. Im parsing json to json

{
"arrayvalues": [
{
"id": 1,
"tot": 2,
"vu": "100",
"props": [
{
"find": "ghi",
"sky": "1000"
},
{
"find": "ijk",
"sky": "10"
}
]
},
{
"id": 2,
"tot": 1,
"vu": "200",
"props": [
{
"find": "abc",
"sky": "100"
},
{
"find": "ijk",
"sky": "20"
}
]
},
{
"id": 3,
"tot": 10,
"vu": "300",
"props": [

{
"find": "def",
"sky": "2000"
},
{
"find": "ijk",
"sky": "30"
}
]
},
{
"id": 4,
"tot": 5,
"vu": "400",
"props": [
{
"find": "xyz",
"sky": "900"
},
{
"find": "ijk",
"sky": "40"
}
]
}
]
}

  1. First i have to check the field called "find". If "find" value is equal to "abc". Then i need to get the "sky" field value(That 100 in 2nd lineitem)
  2. And i need to map this "sky" field value with another lineitems "vu" field values. If it is true then output will be like below (in our scenario 2nd lineitem "sky" value is matching with 1st lineitem "vu" value 100)

{
"id": "2",
"Mark": "No"
},
{
"id": "1",
"Mark": "Yes"
}

 

I have tried the below code. It is working for 1st and 2 nd lineitems(2nd lineitem "sky" field value is matching with 1st lineitem "vu" value 100).

{% assign sky_name = "" %}
{
"values": [
{% for i in content.arrayvalues %}
{% for properties in i.props %}
{% if properties.find == "abc" %}
{
"id" : "{{i.id}}",
"Mark" : "NO"
},
{% assign sky_name = properties.sky %}
{% for j in content.arrayvalues %}
{% assign vu_name = j.vu %}
{% if vu_name == sky_name %}
{
"id" : "{{j.id}}",
"Mark" : "Yes"
},
{% endif %}
{% endfor %}
{% endif %}
{% endfor %}
{% endfor %}
]
}

 

And here 3rd and 4th lineitems "vu" values are not matching with the "sky" field value. Here, I am unable to get these 3rd and 4th lineitems in ouput.
The output for this 3rd and 4th lineitems wil be like below.

{
"id": "3",
"Mark": "NO",
},
{
"id": "4",
"Mark": "NO",
}

 

And my total output will be like below:

{
"arrayvalues" : [
{
"id": "1",
"Mark": "Yes",
},
{
"id": "2",
"Mark": "NO",
},
{
"id": "3",
"Mark": "NO",
},
{
"id": "4",
"Mark": "NO",
}
]
}

Please help me to resolve this issue....

Regards,

Vijay

0 Likes