0

Is there a way to use fstring to change variable dynamically in a complex json object like this:

payload = json.dumps({
   "query": "query ($network: EthereumNetwork!, $dateFormat: String!, $from: ISO8601DateTime, $till: ISO8601DateTime) {\n  ethereum(network: $network) {\n    transactions(options: {asc: \"date.date\"}, date: {since: $from, till: $till}) {\n      date: date {\n        date(format: $dateFormat)\n      }\n      count: countBigInt\n      gasValue\n    }\n  }\n}\n",
   "variables": "{\n  \"limit\": 10,\n  \"offset\": 0,\n  \"network\": \"ethereum\",\n  \"from\": \"2022-11-25T23:59:59\",\"till\":\"2022-11-28T23:59:59\",\n  \"dateFormat\": \"%Y-%m-%d\"\n}"
})

I am trying to change the \"from\": \"2022-11-25T23:59:59\" section, to input a string date variable but running into many problems as the numerous brackets and the embedded strings are making it somewhat difficult when using fstring.

I am also open to any alternative ideas other than fstrings if it fixes the problem

Bemz
  • 129
  • 1
  • 16
  • `variables` is itself a nested JSON object. Just use `json.loads` to convert that to a Python dict, then tweak the dict, then use `json.dumps` to convert it back again. – Tim Roberts Nov 28 '22 at 22:38
  • Yes, it is possible to do this with f-string, but it's ill-advised because of the problem you describe. It's very difficult to get all of the brackets, slashes, quotes, etc. correct within the f-string. Use a tool designed for this purpose, as @TimRoberts suggests. – Michael Ruth Nov 28 '22 at 22:41
  • First, enclose your string in single quotes instead of double to eliminate these ugly escaped quotes. Then leave everything as it is, select the date string (`2022-...-:59`) and replace it with `{my_date_string}`. If you have a `datetime` object instead of string, you can use `{my_datetime:%Y-%m-%dT%H%M%S}` or `{my_datetime.isoformat(timespec="minutes")}`. Finally, add `f` prefix to the string. However, `json` module should be preferred, if possible. – STerliakov Nov 28 '22 at 22:42

1 Answers1

1

As I suggested, just convert the nested JSON to a dict and manipulate it:

import json

payload = {
   "query": "query ($network: EthereumNetwork!, $dateFormat: String!, $from: ISO8601DateTime, $till: ISO8601DateTime) {\n  ethereum(network: $network) {\n    transactions(options: {asc: \"date.date\"}, date: {since: $from, till: $till}) {\n      date: date {\n        date(format: $dateFormat)\n      }\n      count: countBigInt\n      gasValue\n    }\n  }\n}\n",
   "variables": "{\n  \"limit\": 10,\n  \"offset\": 0,\n  \"network\": \"ethereum\",\n  \"from\": \"2022-11-25T23:59:59\",\"till\":\"2022-11-28T23:59:59\",\n  \"dateFormat\": \"%Y-%m-%d\"\n}"
}

var = json.loads(payload['variables'])
var['from'] = var['from'].replace('2022','2024')
payload['variables'] = json.dumps(var)
print(json.dumps(payload))

Output:

{"query": "query ($network: EthereumNetwork!, $dateFormat: String!, $from: ISO8601DateTime, $till: ISO8601DateTime) {\n  ethereum(network: $network) {\n    transactions(options: {asc: \"date.date\"}, date: {since: $from, till: $till}) {\n      date: date {\n        date(format: $dateFormat)\n      }\n      count: countBigInt\n      gasValue\n    }\n  }\n}\n", "variables": "{\"limit\": 10, \"offset\": 0, \"network\": \"ethereum\", \"from\": \"2024-11-25T23:59:59\", \"till\": \"2022-11-28T23:59:59\", \"dateFormat\": \"%Y-%m-%d\"}"}
Tim Roberts
  • 48,973
  • 4
  • 21
  • 30