简体   繁体   English

将 JSON 对象的数组转换为 pyspark 中的字符串

[英]Convert array of JSON objects to string in pyspark

I have one requirement in which I need to create a custom JSON from the columns returned from one PySpark dataframe. So I wrote one UDF like the below which will return a JSON in String format from UDF for each row.我有一个要求,我需要从一个 PySpark dataframe 返回的列中创建一个自定义 JSON。所以我写了一个如下所示的 UDF,它将从 UDF 的每一行返回字符串格式的 JSON。

Parameter "entities" are in the array of JSON format.参数“实体”在 JSON 格式的数组中。

def halResponse(entities, admantx, copilot_id): 
  json_resp = "{\"analyzedContent\": {"+json.dumps(entities)+"}}"
  return json_resp

But in the response, I am not getting proper JSON ie instead of proper key: value pair, I am just getting values(actual values replace with * for security purpose), not key and value.但在响应中,我没有得到正确的 JSON 即不是正确的键:值对,我只是得到值(实际值替换为 * 出于安全目的),而不是键和值。

Find the sample response:查找示例响应:

  "analyzedContents": [
    {
      "entities": [
        [
          "******",
          *,
          *********,
          [
            [
              "***********",
              "***********",
              "***********",
              [
                "*****************"
              ],
              **********
            ]
          ],
          "**************"
        ]
      ]
    }
  ]
}

Please help me to resolve this issue.请帮我解决这个问题。 After fixing, I should get the below sample response修复后,我应该得到以下示例响应

  "analyzedContents": [
    {
      "entities": [
        [
          "key":******",
          "key":*,
          "key":*********,
          [
            [
              "key":"***********",
              "key":"***********",
              "key":"***********",
              [
                "key":"*****************"
              ],
              "key":**********
            ]
          ],
          "key":"**************"
        ]
      ]
    }
  ]
}

Try this without using an UDF:在不使用 UDF 的情况下尝试此操作:

import pyspark.sql.functions as F

df2 = df.withColumn(
    'response',
    F.concat(
        F.lit("{\"analyzedContent\": {"),
        F.to_json(F.col("entities")),
        F.lit("}}")
    )
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM