Search code examples
escapingpowerbipowerquerym

Power Query: Extract substring containing quotation marks from a long string with many quotation marks inside


{"c": "4343434", "d": "m", "k": "+la+la", "l": "40990", "m": "b", "n": "o",
"p": "{adposition}", "s": "b", "dm": "{devicemodel}", "adc": "{creative}", 
"adg": "454554545454", "src": "hahahah45", "tid": "kwd-utututututu", 
"utm_term": "+ka +ka", "utm_medium": "cpc"}

I am trying to extract the section "s":"b" into a new column in a Power Query query. I know this comes down to escaping and I've read the official documentation of the M formula language but I just don't get how to extract this using the text formulas only. How can I do this?


Solution

  • @ShiYang has key function needed.

    If you have a table where a column called Text where each row has text like in your post, then you can write a custom column that extracts the value belonging to s using the following formula:

    = Json.Document([Text])[s]
    

    This takes the text in column [Text] and reads it as JSON and extracts the s component.


    The full step code looks like this:

    = Table.AddColumn(#"Preveious Step", "Custom", each Json.Document([Text])[s])