Reputation: 35
I am parsing a JSON file that is located remotely and which gets updated constantly with new data. I am not going to write the whole structure of the JSON file, cause it would be too much. So far I can get and display names and images on the template, it is straightforward, but what I don't know is how to access some elements with an attribute and with a leading underscore.
Let see part of the JSON file:
[{ 'searchResult' : [{'@count': '5',
'item': [{
'itemId': ['352565431707'],
'title': ['Smart Tab ST8200 Quad-core 7" HD Tablet Android 8.1 Oreo 8GB'],
'galleryURL': ['http://thumbs4.flickr.com/m/maf6ZygaFcxqMsVv2DQTh6g/140.jpg'],
'sellingStatus': [{'currentPrice': [{'@currencyId': 'USD', '__value__': '49.0'}],
'itemURL': ['http://rover.somewebsite.com?item=352565431707']}]
'item': [{
'itemId': ['352565431808'],
'title': ['iPhone 11 latest phone '],
'galleryURL': ['http://thumbs4.flickr.com/m/maf6ZygaFcxqMsVv2DQOm6g/140.jpg'],
'sellingStatus': [{'currentPrice': [{'@currencyId': 'USD', '__value__': '1200.0'}],
'itemURL': ['http://rover.somewebsite.com?item=352565431808']}]
This is part of my code in views.py
url = 'http://svcs.somewebsite.com?RESPONSE-DATA-FORMAT=JSON&keyword=something'
r = requests.get(url).json()
search =r['searchResult'][0]['item']
context = { 'items': search }
template='home.html'
return render(request, template, context)
This is part of my code in home.html template
{% for item in items %}
<a href = "{{ item.itemURL.0 }} />
<img src="{{ item.galleryURL.0 }}" alt="{{ item.title.0 }}" />
</a>
<h2>{{ item.title.0 }}</h2>
<h3>{{ item.sellingStatus.0.currentPrice.0.__value__.0 }}</h3>
{% endfor %}
According to what I have it renders the image and the title, but when I try to access the __value__, because of the leading underscore I get an error message, and honestly I don't know if I am accessing the attribute properly with this approach. Back to my question, How can I access the attribute in this particular case and how can escape the leading underscore ? Thank you so much in advance for any help.
Upvotes: 0
Views: 1144
Reputation: 35
Since I love to share what I know with the world here is the answer to my own question. After doing some research and scratching my head for days, I came up with this solution, that works great for my case.
In order to access the attribute elements and the leading underscore, I had to clean up the data in views and do the iteration in views as well. I created four variable-containers that i would use to append every result of that iteration. If you don't do this, you will end up getting only the last result of the iteration in the template.
Upvotes: 0
Reputation: 90
Friend, did you manage to solve the problem? If not, a possible easier solution would be using javascript. Try to somehow store the tag variable {{items}} in a javascript variable.
for (let item in variableItems){
let currentPrice = variableItems[item].sellingStatus[0].currentPrice[0];
console.log(currentPrice['@currencyId'];
console.log(currentPrice['__value__'];
}
and once done this use the values for the purpose you want.
Another possible solution would be in the backend to remove the @ character and the __ (since the @ character generates problems in the template). You can use the replace function. str.replace('@', '')
and str.replace('__' , '')
y en la pantilla html:
{{ item.sellingStatus.0.currentPrice.0.currencyId}}
{{ item.sellingStatus.0.currentPrice.0.value}}
Upvotes: 0
Reputation: 90
Friend, you have to use the same variable declared in FOR loop. You must use item variable instead of items. Technically items does not exist in that area:
{% for item in items %}
<a href="{{item .viewItemURL.0}}">
<img src="{{ item .galleryURL.0 }}" alt="{{ item .title.0 }}" />
</a>
<p>Names: {{ item .title.0 }}</p>
{% endfor %}
Upvotes: 1