How to create a JSON nested where the objects have different names

Six Stars

How to create a JSON nested where the objects have different names

Hello guys!

 

I have the following table structure and I'd like to create a JSON file using the tWriteJSONField.

 

FirstName | LastName
John      | McCain
Peter     | Fitzgerald
Pablo     | De Los Santos

 

My JSON should be something like that below. The name of the nest could be created using the Numeric.sequence function I suppose.

{
    "0" : {
        "FirstName" : "John",
        "LastName" : "McCain"
    },
    "1" : {
        "FirstName" : "Peter",
        "LastName" : "Fitzgerald"
    },
    "2" : {
        "FirstName" : "Pablo",
        "LastName" : "De Los Santos"
    }
}

 

Thanks for helping me.

 

 


Accepted Solutions
Seventeen Stars

Re: How to create a JSON nested where the objects have different names

You can do such things - and more - with the component tJSONDocOutput. Get them in Talend Exchange or Github: https://github.com/jlolling (more up-to-date) 


All Replies
Community Manager

Re: How to create a JSON nested where the objects have different names

Hello
The element name must be defined before the job execution and it can't be a variable, so it is impossible to generate a json string like you showed with tWriteJsonField.

Regards
Shong

----------------------------------------------------------
Talend | Data Agility for Modern Business
Seventeen Stars

Re: How to create a JSON nested where the objects have different names

You can do such things - and more - with the component tJSONDocOutput. Get them in Talend Exchange or Github: https://github.com/jlolling (more up-to-date) 

Six Stars

Re: How to create a JSON nested where the objects have different names


jlolling wrote:

You can do such things - and more - with the component tJSONDocOutput. Get them in Talend Exchange or Github: https://github.com/jlolling (more up-to-date) 


Thanks a milion! Smiley Happy

Eight Stars

Re: How to create a JSON nested where the objects have different names

HI @jlolling,

 

Can we get a sample job design how to do the above structure.

 

 

Thanks,

Bharath.

Seventeen Stars

Re: How to create a JSON nested where the objects have different names

This is still not very easy. The output component can use values as field names but the current version is not flexible enough to add per row one new attribute+object. I have in my current project a similar problem and will solve this by update the components in the next days.

Seventeen Stars

Re: How to create a JSON nested where the objects have different names

I can currently create this:

[ {
  "0" : {
    "firstname" : "John",
    "lastname" : "McCain"
  }
}, {
  "1" : {
    "firstname" : "Peter",
    "lastname" : "Fritzgerald"
  }
}, {
  "2" : {
    "firstname" : "Pablo",
    "lastname" : "De Los Santos"
  }
} ]
Seventeen Stars

Re: How to create a JSON nested where the objects have different names

I have solved the issue! In the next days I will test it if there is no unwanted impact to other parts and publish the new version.

There is no change within the configuration of a component.

{
  "0" : {
    "firstname" : "John",
    "lastname" : "McCain"
  },
  "1" : {
    "firstname" : "Peter",
    "lastname" : "Fritzgerald"
  },
  "2" : {
    "firstname" : "Pablo",
    "lastname" : "De Los Santos"
  }
}

tJSONDocOutput_example_dynamic_objects.png

Eight Stars

Re: How to create a JSON nested where the objects have different names

HI @jlolling,

Thanks for the reply. I will take this as reference and try to complete and let you know.

Once again thanks for the quick response.

 

Thanks,

Bharath.

Seventeen Stars

Re: How to create a JSON nested where the objects have different names

I have just published on Talend Exchange and Github the new version (14.0) of the tJSONDoc components which allows the described job design.

https://exchange.talend.com/#marketplaceproductoverview:marketplace=marketplace%252F1&p=marketplace%...

https://github.com/jlolling/talendcomp_tJSONDoc/releases

Eight Stars

Re: How to create a JSON nested where the objects have different names

Hi @jlolling,

 

We have tried the example which you sent but we are unable to get the expected json. Below are the issues

For parent json path you mentioned String.valueOf(row2.index), we used same way but we are getting null value. And one more thing we are getting only one value in object but here we need to get multiple values as object. I don't see an multiple objects in an object option. Could help me on this.

 

Source:

address_id,address_line_1,city,address_type
12345,1000 Main St.,Boston,home
6789,1000 Work Dr.,Boston,work

 

Expected Output:

"address":
{
"home":
{
"address_id":"123245",
"address_line_1":"1000 Main St.",
"city":"Boston",
"country":"US",
"state":"MA"
},
"work":
{
"address_id":"6789",
"address_line_1":"1000 Work Dr.",
"city":"Boston",
"country":"US",
"state":"MA"
}
}

 

Thanks,

Bharath.

Seventeen Stars

Re: How to create a JSON nested where the objects have different names

It was quite easy to do:

{
  "home" : {
    "address_id" : "12345",
    "address_line_1" : "1000 Main St.",
    "city" : "Boston"
  },
  "work" : {
    "address_id" : "6789",
    "address_line_1" : "1000 Work Dr.",
    "city" : "Boston"
  }
}

I have attached the demo job as item export. Use the item import to load this job into your studio. The job is created with Talend 6.5.1

Four Stars

Re: How to create a JSON nested where the objects have different names

@jlolling Hi, Can you send us the sample job you created for this scenario :

Scenario 2: Example of multi-level document creation

 

I need to see how data look like in tfixedfileinput.