在 power query 的 Json 文件中为数字添加引号

Add quotes around numbers in Json file in power query

我有一个 json 文件需要解析,但我必须在 power query 评估文件之前将特定列从数字转换为文本。我必须这样做,因为这个特定的列是数字格式导致 power query 错误地评估一些记录。

我能够从堆栈溢出的某人那里获得下面列出的代码,作为如何完成此转换的建议。

let  Source = Lines.FromBinary(File.Contents("C:\temp\a.json"), null, null, 1252),
p=List.Transform(List.Positions(Source), each 
    if _ =0 then Source{_} else 
    if Text.Contains(Text.From(Source{_-1}),"provider_references") then """" & Text.Trim(Text.From(Source{_}))& """"  else Source{_}
),
newJson=Json.Document(Text.Combine(p,"#(lf)"))
in newJson

但是,当我 运行 此代码时,JSON 文件永远不会加载到 excel。它只是保持 运行ning 和 运行ning。我猜它陷入了无限循环,但由于我不熟悉电源查询,所以无法调试。任何建议都会很棒。谢谢

请参阅下面 json 文件的结构

{
    "reporting_entity_name": "test",
    "reporting_entity_type": "testr",
    "last_updated_on": "2022-05-05",
    "version": "1.0.0",
    "provider_references": [
        {
            "provider_group_id": 380.1,
            "provider_groups": [
                {
                    "npi": [
                        9999999999
                    ],
                    "tin": {
                        "type": "ein",
                        "value": "57-999999999"
                    }
                }
            ]
        }
    ],
    "in_network": [
        {
            "negotiation_arrangement": "ffs",
            "name": "test",
            "billing_code_type": "RC",
            "billing_code_type_version": "2022",
            "billing_code": "xxxx",
            "description": "test",
            "negotiated_rates": [
                {
                    "provider_references": [
                        380.61
                    ],
                    "negotiated_prices": [
                        {
                            "negotiated_type": "negotiated",
                            "negotiated_rate": 0.00,
                            "expiration_date": "9999-12-31",
                            "service_code": [
                                "22"
                            ],
                            "billing_class": "institutional",
                            "billing_code_modifier": []
                        }
                    ]
                }
            ]
        },
        {
            "negotiation_arrangement": "ffs",
            "name": "test",
            "billing_code_type": "RC",
            "billing_code_type_version": "2022",
            "billing_code": "zzzz",
            "description": "test",
            "negotiated_rates": [
                {
                    "provider_references": [
                        380.60
                    ],
                    "negotiated_prices": [
                        {
                            "negotiated_type": "negotiated",
                            "negotiated_rate": 105.00,
                            "expiration_date": "9999-12-31",
                            "service_code": [
                                "22"
                            ],
                            "billing_class": "institutional",
                            "billing_code_modifier": ["00"
                            ]
                        }
                    ]
                }
            ]
        }
        
    ]
}

在任何以 380 开头的数字周围加上引号,以转换为字符串

let Source = Lines.FromBinary(File.Contents("C:\temp\a.json"), null, null, 1252),
fnRegexExtr=(text,regex)=>Web.Page("<script>var x='"&text&"';var y=new RegExp('"&regex&"','g');var b=x.match(y);document.write(b);</script>")[Data]{0}[Children]{0}[Children]{1}[Text]{0} ,
p=List.Transform(List.Positions(Source), each 
if Text.Contains(Source{_},"380") then 
Text.Replace(Source{_},fnRegexExtr(Source{_},"380\.?[0-9]*"),""""&fnRegexExtr(Source{_},"380\.?[0-9]*")&"""")
else Source{_}
),
newJson=Json.Document(Text.Combine(p,"#(lf)"))
in newJson