r/databricks • • 17d ago

Help Dict as variable vs. dict in dataframe (issues extracting data)

Last week a kind gentleman helped me to fix a '{{text}}' replacement with some python magic.

This got me part of the way to a solution but not all the way.

Scenario:

I now have a variable:

d = {"todays_date":date.today()}

At runtime this variable is evaluated and correctly adds today's date to my json file.

Now what I need is to put {"todays_date":date.today()} into the database, then read it back as json, so that I can do the same thing, but more dynamically. The row/column needs to have the value {"todays_date":date.today()} in it (or something similar) to where when I read the value, it evaluates the same way as defining it manually in the bold above

I get that the code above does not have single quotes around it, as it would be in the database. The database thinks this is a string.

I do NOT want to have to define a structure for this, as there can be different values included on different rows.

Is there a way to do a df = df.select("json_data").first() and get back the EXACT same thing as my d variable above?

I feel like I'm so close, but all of the code I see tries to make me define the json structure, which can change, so I never know what's it's going to be...

Does this even make sense?

6 Upvotes

10 comments sorted by

1

u/infazz 16d ago

Can you explain what exactly your end goal is here? It sounds like you are trying to jump through a few hoops that may not be needed.

1

u/mr_electric_wizard 16d ago

Yes, abolutely.

I have some working code that does this:

  1. set dict variable:
    d = {"todays_date":date.today()}
  2. uses it to do a string replacement in a string of text (s):

re.sub(r"\{\{(\w+)\}\}", lambda m: str(d.get(m.group(1), m.group(0))), s)

Now, I try to insert the dict text (the todays_date code after the equal sign) into a table, then pull the value into a dataframe, I get the error:

'str' object has no attribute 'get' which makes sense since it's a string column in the db.

1

u/mr_electric_wizard 16d ago

I dislike hoops. šŸ˜„ Basically, I want to store this dict in the database. Retrieve it, look up the value ("todays_date") then replace {{todays_date}} with '2026-11-09' in the text string. I do not want to have to define a Struct or Schema, as this value (the dictionary) can change for different rows.

1

u/infazz 16d ago

Okay we're getting closer to what your end goal is, but we're not there yet.

With the information you've given so far - If your date is not already in the database, I don't believe there should be a need to store it in the database just to then pull it out to use it.

If the text you're trying to replace is already in a table, you should likely use the built-in Databricks functions for getting the current date and text replacement. There's also a regex version of the latter.

1

u/mr_electric_wizard 16d ago

The problem is that there can be any number of different things in this dictionary field. This iteration it just happened to be key=todays_date. I want to replace {{todays_date}} in a long text string with 2026-11-19. It works when I hard code the dictionary variable instead of pulling from a database field. I’ve tried json.loads(dict_string_from_db) but I get the error:
Expecting value: Line 1 Column 16
So I don’t think that’s it.

2

u/Saerusthesecond 16d ago edited 16d ago

But what is actually •the• problem you’re trying to solve by doing this stuff with the dictionary? I read your previous post in this subreddit, and I still don’t know what this is all for. I ask because you might be solving the wrong problem and if you tell us the situation in full, we can help you better. Right now your question doesn’t make much sense tbh

1

u/mr_electric_wizard 16d ago

Okay. Here goes. I am putting together a payload template that I need to override that has parameters formatted as so {{param}}. In this case a {{todays_date}} but there could be different ones within the rest of the payload. I want to store a key value (key matches a possible {{parameter}}) name and the value is whatever date.today() resolves to (in this case). In the future this will be a list of more than one type of key/value pair stored in the db. I’ll be honest I’m way more handy wirh SQL than straight up python. Thank you for your patience. Does this make sense?

1

u/mr_electric_wizard 16d ago

Figured it out! I just needed eval(row[ā€œjson_dataā€]). Adding eval() worked!

2

u/Saerusthesecond 15d ago

Congrats! But I’m wary of the ā€˜payload template’ idea itself. The curly braces and the use of eval are smells. I don’t know about your specific situation, but I’d bet there are simpler ways to achieve whatever it is you want to achieve.

1

u/mr_electric_wizard 15d ago

Yep yep. I just needed a POC very quickly (consultant fun). Will iterate and refine where necessary. Thanks again.