<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Object of type bool_ is not JSON serializable in Data Engineering</title>
    <link>https://community.databricks.com/t5/data-engineering/object-of-type-bool-is-not-json-serializable/m-p/13133#M7852</link>
    <description>&lt;P&gt;Thanks but not sure how do I "write json directly from dataframe without dict and looping".&lt;/P&gt;&lt;P&gt;df1.Account__r or df1.Product_Master__r simply won't work as there are no such objects as "Account__r " or "Product_Master__r" in a dataframe. That's why I used dict to create it.&lt;/P&gt;</description>
    <pubDate>Mon, 18 Oct 2021 12:51:57 GMT</pubDate>
    <dc:creator>Braxx</dc:creator>
    <dc:date>2021-10-18T12:51:57Z</dc:date>
    <item>
      <title>Object of type bool_ is not JSON serializable</title>
      <link>https://community.databricks.com/t5/data-engineering/object-of-type-bool-is-not-json-serializable/m-p/13130#M7849</link>
      <description>&lt;P&gt;I am doing a convertion of a data frame to nested dict/json. One of the column called "Problematic__c" is boolean type.&lt;/P&gt;&lt;P&gt;For some reason json does not accept this data type retriving error: "Object of type bool_ is not JSON serializable" &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I need this as boolean as this json is later injected to Salesforce via API. I could easly make it string but the destination object accept boolean only.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Here is a python code:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;all_rows = len(data)
y = []
    
for i in range(all_rows):    
    x = dict(data.iloc[i, 2:])
    
    x["Account__r"] = dict(data.iloc[i, :1])
    x["Product_Master__r"] = dict(data.iloc[i, 1:2])
    y.append(x)
    y=json.dumps(y)&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;and this is expected output:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;[
   {
      "Recommended_Action__c":"Take action Z",
      "Extra_Information_JSON__c":"[{\"name\":\"Action\",\"value\":\"Verifier remplissage\"},{\"name\":\"Stock Disponible\",\"value\":\"18\"}]",
      "Flag__c":"Rupture Ponctuel",
      "Problematic__c":True,
      "Value__c":5800.0,
      "Source_Id__c":"538.0",
      "Batch__c":"a2e7Y110000dO6WQAU",
      "Account__r":{
         "Code__c":"00001-B"
      },
      "Product_Master__r":{
         "EAN_Code__c":"1111111111.0"
&amp;nbsp;     }
    },
    {  
        ....
    }.
    .....
 ]&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Data frame called "data" has structure as below with sample values:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;"Code__c":"00001-B"
"EAN_Code__c":"1111111111.0"
"Recommended_Action__c":"Take action Z",
"Extra_Information_JSON__c":"[{\"name\":\"Action\",\"value\":\"Verifier remplissage\"},{\"name\":\"Stock Disponible\",\"value\":\"18\"}]",
"Flag__c":"Rupture Ponctuel",
"Problematic__c":True,
"Value__c":5800.0,
"Source_Id__c":"538.0",
"Batch__c":"a2e7Y110000dO6WQAU",
"Code__c":"00001-B"
"EAN_Code__c":"1111111111.0"&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 17 Oct 2021 18:52:14 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/object-of-type-bool-is-not-json-serializable/m-p/13130#M7849</guid>
      <dc:creator>Braxx</dc:creator>
      <dc:date>2021-10-17T18:52:14Z</dc:date>
    </item>
    <item>
      <title>Re: Object of type bool_ is not JSON serializable</title>
      <link>https://community.databricks.com/t5/data-engineering/object-of-type-bool-is-not-json-serializable/m-p/13132#M7851</link>
      <description>&lt;P&gt;Hi I had similar problem with boolean but with export to different data format. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;please try to write json directly from dataframe without dict and looping (all needed transformation can be done in dataframe):&lt;/LI&gt;&lt;/UL&gt;&lt;PRE&gt;&lt;CODE&gt;df2 = df1.select(df1.Account__r, df1.Product_Master__r)
df2.coalesce(1).write.format('json').save('/path/file_name.json')&lt;/CODE&gt;&lt;/PRE&gt;&lt;UL&gt;&lt;LI&gt;you can also write spark dataframe also directly to Salesforce please check &lt;A href="https://github.com/springml/spark-salesforce" target="test_blank"&gt;https://github.com/springml/spark-salesforce&lt;/A&gt;&lt;/LI&gt;&lt;/UL&gt;</description>
      <pubDate>Mon, 18 Oct 2021 10:31:31 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/object-of-type-bool-is-not-json-serializable/m-p/13132#M7851</guid>
      <dc:creator>Hubert-Dudek</dc:creator>
      <dc:date>2021-10-18T10:31:31Z</dc:date>
    </item>
    <item>
      <title>Re: Object of type bool_ is not JSON serializable</title>
      <link>https://community.databricks.com/t5/data-engineering/object-of-type-bool-is-not-json-serializable/m-p/13133#M7852</link>
      <description>&lt;P&gt;Thanks but not sure how do I "write json directly from dataframe without dict and looping".&lt;/P&gt;&lt;P&gt;df1.Account__r or df1.Product_Master__r simply won't work as there are no such objects as "Account__r " or "Product_Master__r" in a dataframe. That's why I used dict to create it.&lt;/P&gt;</description>
      <pubDate>Mon, 18 Oct 2021 12:51:57 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/object-of-type-bool-is-not-json-serializable/m-p/13133#M7852</guid>
      <dc:creator>Braxx</dc:creator>
      <dc:date>2021-10-18T12:51:57Z</dc:date>
    </item>
    <item>
      <title>Re: Object of type bool_ is not JSON serializable</title>
      <link>https://community.databricks.com/t5/data-engineering/object-of-type-bool-is-not-json-serializable/m-p/13134#M7853</link>
      <description>&lt;P&gt;you can achieve it by transforming dataframe using built-in spark functions etc.&lt;/P&gt;</description>
      <pubDate>Mon, 18 Oct 2021 14:39:31 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/object-of-type-bool-is-not-json-serializable/m-p/13134#M7853</guid>
      <dc:creator>Hubert-Dudek</dc:creator>
      <dc:date>2021-10-18T14:39:31Z</dc:date>
    </item>
    <item>
      <title>Re: Object of type bool_ is not JSON serializable</title>
      <link>https://community.databricks.com/t5/data-engineering/object-of-type-bool-is-not-json-serializable/m-p/13135#M7854</link>
      <description>&lt;P&gt;You can just use `to_json` to achieve this. Here is an example:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;from pyspark.sql import Row
from pyspark.sql.types import *
from pyspark.sql.functions import to_json
&amp;nbsp;
data = [(1, Row(Code__c="00001-B", 
                EAN_Code__c="1111111111.0",
                Extra_Information_JSON__c="[{\"name\":\"Action\",\"value\":\"Verifier remplissage\"},{\"name\":\"Stock Disponible\",\"value\":\"18\"}]",
                Flag__c="Rupture Ponctuel",
                Problematic__c=True))]
&amp;nbsp;
df = spark.createDataFrame(data, ("key", "value"))
display(df.select(to_json(df.value).alias("json")))&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;This is just an example to point you in the right direction, you may need to adapt it to your specific input format. This is meant to run in a Databricks notebook, otherwise the final `display` will not work.&lt;/P&gt;</description>
      <pubDate>Mon, 18 Oct 2021 18:01:02 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/object-of-type-bool-is-not-json-serializable/m-p/13135#M7854</guid>
      <dc:creator>Dan_Z</dc:creator>
      <dc:date>2021-10-18T18:01:02Z</dc:date>
    </item>
    <item>
      <title>Re: Object of type bool_ is not JSON serializable</title>
      <link>https://community.databricks.com/t5/data-engineering/object-of-type-bool-is-not-json-serializable/m-p/13136#M7855</link>
      <description>&lt;P&gt;Thanks Dan, that make sens!&lt;/P&gt;</description>
      <pubDate>Fri, 22 Oct 2021 08:16:15 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/object-of-type-bool-is-not-json-serializable/m-p/13136#M7855</guid>
      <dc:creator>Braxx</dc:creator>
      <dc:date>2021-10-22T08:16:15Z</dc:date>
    </item>
    <item>
      <title>Re: Object of type bool_ is not JSON serializable</title>
      <link>https://community.databricks.com/t5/data-engineering/object-of-type-bool-is-not-json-serializable/m-p/13137#M7856</link>
      <description>&lt;P&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Thu, 04 Nov 2021 01:51:55 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/object-of-type-bool-is-not-json-serializable/m-p/13137#M7856</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2021-11-04T01:51:55Z</dc:date>
    </item>
  </channel>
</rss>

