The Template in JINJA2 bulk replaces the contents of a JSON string

Source: Internet
Author: User
Tags convert json to string

The project uses the Elasticsearch, using JSON format Query method, there are several places in a query statement need to replace, and replace the same value. The first way to convert JSON to string, using the Format function, to find that back to JSON is not successful, colleagues remind you can use the JINJA2 template processing strings,

Second_search = "" "

    {
"Query": {
"bool": {
"should": [
{
"bool": {
"should": [
{
"term": {
"{{prod}}_name_en.text": {
"value": "{{keyword}}",
"Boost": Ten
}
}
},
{
"term": {
' {{prod}}_name_zh.text ': {
"value": "{{keyword}}",
"Boost": Ten
}
}
}
]
}
},
{
"bool": {
"should": [
{
"Wildcard": {
' {{prod}}_name_en.text ': {
"value": "{{keyword}}*",
"Boost": 5
}
}
},
{
"Wildcard": {
' {{prod}}_name_zh.text ': {
"value": "{{keyword}}*",
"Boost": 5
}
}
}
]
}
},
{
"Query_string": {
' Fields ': [' {{prod}}_name_en ', ' {{Prod}}_name_zh '],
"Query": "{{keyword}}",
"Boost": 1
}
}
]
}
}
}
"""
SECOND_TPL = Template (Second_search)
Second_str = Second_tpl.render (Keyword=kws, prod= ' other ')
Second_json = Json.loads (SECOND_STR)
It's going to be nice to turn the string into a JSON object after this processing.

The Template in JINJA2 bulk replaces the contents of a JSON string

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.