Home > front end >  Add data to a json file using Talend
Add data to a json file using Talend

Time:12-08

I have the following JSON:

[
    {
        "date": "29/11/2021",
        "Name": "jack",

    },
    {
        "date": "30/11/2021",
        "Name": "Adam",

    },
        "date": "27/11/2021",
        "Name": "james",

    }
]

Using Talend, I wanna add 2 lines to have something like:

[
    {
        "company": "AMA",
        "service": "BI",
        "date": "29/11/2021",
        "Name": "jack",
        

    },
    {
        "company": "AMA",
        "service": "BI",    
        "date": "30/11/2021",
        "Name": "Adam",

    },
        "company": "AMA",
        "service": "BI",    
        "date": "27/11/2021",
        "Name": "james",

    }
]

Currently, I use 3 components (tJSONDocOpen, tFixedFlowInput, tJSONDocOutput) but I can't have the right configuration of components in order to get the job done !

CodePudding user response:

If you are not comfortable with json .

Just do these steps :

  1. In the metaData just create a FileJson like this then paste it in your job as a tFileInputJson

enter image description here

  1. Your job design and mapping would be

enter image description here

  1. In your tFileOutputJson don't forget to change in the name of the data block "Data" with ""

CodePudding user response:

What you need to do there according to the Talend practices is read your JSON, then extract each object of it, add your properties and finally rebuild your JSON in a file.

An efficient way to do this is using tMap componenent like this

The first tFileInputJSON will have to specify what properties it has to read from the JSON by setting your 2 objects in the mapping field

Then the tMap will simply add 2 columns to your main stream, here is an example with hard coded string values. Depending on you needs, this component will also offer you the possibility to assign dynamic data to your 2 new columns, it's a powerful tool for manipulating the structure of a data stream.

You will find more infos about this component in the official documentation : https://help.talend.com/r/en-US/7.3/tmap/tmap ; especially the "tMap scenarios" part

note

Instead of using the tMap, if you are easy with Java, you can use a tjavaRow instead. Using this, you can setup your 2 new columns with whatever java code you want to put as long as you have defined the output schema of the component.

output_row.Name = input_row.Name;
output_row.date = input_row.date;
output_row.company = "AMA";
output_row.service = "BI";
  • Related