0

I have data in the MongoDB database where one of the columns is JSON Array. Now I want to retrieve only the first object from that JSON Array column.

Input_Column :

**[{"id" : "25",
"name" : "spot1" ,
"node" : "start_node", 
"order" : "1",
"status" : "completed", 
"location" : "gate1"},
{"name" : "spot2", 
"order" : "2" },
{ "name" : "spot3", 
"node" : "start_node", 
"status" : "pending" }]**

Required output_column :

 `status:`

 completed 

I need only the first value from the array. I tried using the tExtractJSONFields component in Talend. And set the JSON loop as "$.status". But I got the output as

`status:`

completed
null
pending 

AFTER EDIT :

[{"id" : "25",
"name" : "spot1" ,
"node" : "start_node", 
"order" : "1",
"status" : "completed" ,
"location" : "gate1"},
{"name" : "spot2", 
"order" : "2" },
{ "name" : "spot3", 
"node" : "start_node", 
"status" : "pending" } ]


[{"id" : "26",
"name" : "pull1" ,
"node" : "start_node", 
"order" : "3",
"status" : "arrived" ,
"location" : "gate3"},
{"name" : "pull2", 
"order" : "4" },
{ "name" : "pull3", 
"node" : "end_node", 
"status" : "pending" } ]

[{"id" : "27",
"name" : "task1" ,
"node" : "start_node", 
"order" : "5",
"status" : "pending" ,
"location" : "gate12"},
{"name" : "task2", 
"order" : "6" },
{ "name" : "task3", 
"node" : "end_node", 
"status" : "pending" } ]

expected output :

status 

completed
arrived
pending
2

1 Answer 1

0

I think that this is not a valid json you have post

I corrected your json to be like this

You can test it here : https://jsonformatter.curiousconcept.com/#

[{"id" : "25",
"name" : "spot1" ,
"node" : "start_node", 
"order" : "1",
"status" : "completed" ,
"location" : "gate1"},
{"name" : "spot2", 
"order" : "2" },
{ "name" : "spot3", 
"node" : "start_node", 
"status" : "pending" } ]

Try to put your json in a file at first time
-> then in the metadata

enter image description here

--> drag & drop your file json (Metadata)

In your job just put this structure enter image description here

javaFlex is configured as such enter image description here

  • In the advanced settings don't forget to put :

import java.util.ArrayList;

Output would be :

Starting job Test_ at 14:36 27/05/2021. [statistics] connecting to socket on port 3903 [statistics] connected completed [statistics] disconnected

Job Test_ ended at 14:36 27/05/2021. [exit code = 0]

Sign up to request clarification or add additional context in comments.

7 Comments

I need to get the first value from status. How do I achieve that? status = completed
Just edited my answer ! this is how you could achieve it
Thank you so much. This works for one record. I have multiple records where the first element from all the JSON arrays must be retrieved.
Just adapt the method using the other columns and delete the first element
I have edited my ques, please check. Thanks
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.