Facing issue KeyError while formatting string which contains JSON

Shiva

In below code I'm facing KeyError, even though my syntax seems fine.

data = {'id': 20720}
query = '{"query":{"bool":{"must":[{"term":{"status.keyword":"Running"}},{"term":{"id":{id}}}],"filter":[{"term":{"status.keyword":"Running"}}]}}}'
print(query.format(**data))
Traceback (most recent call last):
  File ".\demo.py", line 3, in <module>
    print(query.format(**data))
KeyError: '"query"'

Here I'm formatting Elasticsearch query, I don't wanted to use replace string because I have to iterate it multiple time if data contains more field.

I'm expecting below output.

{"query":{"bool":{"must":[{"term":{"status.keyword":"Running"}},{"term":{"id":20720}}],"filter":[{"term":{"status.keyword":"Running"}}]}}}

Val

You need to double your curly braces except the ones around the id variable

query = '{{"query":{{"bool":{{"must"... {{"term":{{"id":{id} }} }}...

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Facing issue while sending "&" in a string

From Dev

facing issue while formating the JSON Data

From Dev

Facing issue with parsing the following JSON String in Python

From Dev

Facing issue while replacing substring in string in a dataframe using python 3.4.4

From Dev

Facing issue while login redirect

From Dev

Facing warning issue while debugging

From Dev

formatting the long sql string which contains double quotes

From Dev

Facing issue "Error Code: EPERM" while conducting a build for hawtio which includes typescript and Angular files

From Dev

Issue while formatting date

From Dev

Unhandled Exception: FormatException: Invalid radix-10 number (at character 1). Facing a issue while converting string to int

From Dev

Facing urlencoding issue while processing request

From Dev

facing issue while populating/referencing the variable in shell

From Dev

Facing issue while deserializing the http resposne

From Dev

Facing issue with MFA while adding account to Yodlee

From Dev

Facing issue while installing ruamel.yaml

From Dev

NodeJs facing the issue while using async waterfall

From Dev

Facing issue while installing Cassandra on MacOs

From Dev

Facing issue while calling @viewchild in angular

From Dev

Facing issue while running Daml scenarios

From Dev

Facing connection issue while running copy in Vertica

From Dev

Facing issue while starting Controller in Docker

From Dev

Facing issue while executing the groovy code in JMeter

From Dev

Facing issue while executing the Groovy code

From Dev

Facing an issue while learning logical ML

From Dev

facing an issue while reading json Line 52: 'myJson' is not defined no-undef

From Dev

In Rest Assured while trying to get token facing issue "Failed to parse the JSON document"

From Dev

Using json file to give appbar logo for flutter. Facing Loading issue while navigation

From Dev

Crazy issue with string formatting

From Dev

Interpolated string formatting issue

Related Related

  1. 1

    Facing issue while sending "&" in a string

  2. 2

    facing issue while formating the JSON Data

  3. 3

    Facing issue with parsing the following JSON String in Python

  4. 4

    Facing issue while replacing substring in string in a dataframe using python 3.4.4

  5. 5

    Facing issue while login redirect

  6. 6

    Facing warning issue while debugging

  7. 7

    formatting the long sql string which contains double quotes

  8. 8

    Facing issue "Error Code: EPERM" while conducting a build for hawtio which includes typescript and Angular files

  9. 9

    Issue while formatting date

  10. 10

    Unhandled Exception: FormatException: Invalid radix-10 number (at character 1). Facing a issue while converting string to int

  11. 11

    Facing urlencoding issue while processing request

  12. 12

    facing issue while populating/referencing the variable in shell

  13. 13

    Facing issue while deserializing the http resposne

  14. 14

    Facing issue with MFA while adding account to Yodlee

  15. 15

    Facing issue while installing ruamel.yaml

  16. 16

    NodeJs facing the issue while using async waterfall

  17. 17

    Facing issue while installing Cassandra on MacOs

  18. 18

    Facing issue while calling @viewchild in angular

  19. 19

    Facing issue while running Daml scenarios

  20. 20

    Facing connection issue while running copy in Vertica

  21. 21

    Facing issue while starting Controller in Docker

  22. 22

    Facing issue while executing the groovy code in JMeter

  23. 23

    Facing issue while executing the Groovy code

  24. 24

    Facing an issue while learning logical ML

  25. 25

    facing an issue while reading json Line 52: 'myJson' is not defined no-undef

  26. 26

    In Rest Assured while trying to get token facing issue "Failed to parse the JSON document"

  27. 27

    Using json file to give appbar logo for flutter. Facing Loading issue while navigation

  28. 28

    Crazy issue with string formatting

  29. 29

    Interpolated string formatting issue

HotTag

Archive