Add an attribute in a json file but in a specific place using databricks

Samuel Pastrana Bedoya 146 Reputation points
2022-08-11T01:25:23.647+00:00

I have a json file which I am performing a data treatment, I must create a new attribute with a specific value, but the created attribute must not be at the end of the message otherwise in a specific place, does anyone know if this addition can be done using pandas or pyspark?
Thank you very much.
230548-esquema-erroneo.png230587-esquema-correcto.png

Azure Data Lake Storage
Azure Data Lake Storage
An Azure service that provides an enterprise-wide hyper-scale repository for big data analytic workloads and is integrated with Azure Blob Storage.
Azure Databricks
Azure Databricks
An Apache Spark-based analytics platform optimized for Azure.
0 comments No comments
{count} votes

Answer accepted by question author
  1. ShaikMaheer-MSFT 38,631 Reputation points Microsoft Employee Moderator
    2022-08-12T09:54:39.737+00:00

    Hi @Samuel Pastrana Bedoya ,

    Thank you for posting query in Microsoft Q&A Platform. JSON is dictionary in python.

    Yes, we can do that easily in python. In your notebook consider writing python to accomplish your requirements.

    In your case you need to remove item "3":"NO" from main dictionary and also add item("3":"YES") to Sub dictionary of property "2".

    Lets say your dictionary is myDic variable then,

    • To remove items from dictionary we should consider using myDic.pop("3")
      • To add items to dictionary we should consider using syntax as (myDic["2"])["3"] = "YES"

    Please check below screenshot.

    230742-image.png

    Code used in above screenshot:

    myDic = {  
        "1":{  
            "a":"",  
            "b":""  
        },  
        "2":{  
            "c":"1",  
            "d":"1"  
        },  
        "3":"NO"  
    }  
    
    myDic.pop("3")  
    
    (myDic["2"])["3"] = "YES"  
    
    print(myDic)  
    

    Please note, position of property "3" inside property "2" does not have any impact as dictionaries in python don't work on index's and we can't have duplicate properties in it.

    Please check below video for better understanding of python dictionaries.
    Dictionaries in Python

    Hope this helps.


    Please consider hitting Accept Answer button. Accepted answers help community as well.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.