tExtractJsonFields - accessing a sibling field at the same level as the loop

Six Stars

tExtractJsonFields - accessing a sibling field at the same level as the loop

sample json:

 

{
"response": {
"code": "200",
"description": "SUCCESS",
"type": "SUCCESS"
},
"agentPartyID": "XLB91",
"agencyId": "X0945",
"agencyName": "CRUMP LIFE INSURANCE",
"producerHierarchy": [{
   "primaryAgentPartyID": "XLB92"
   }, {
   "primaryAgentPartyID": "SM001"
   }, {
   "primaryAgentPartyID": "BBBBBBBBBB"
   }, {
   "primaryAgentPartyID": "X0559"
   }, {
   "primaryAgentPartyID": "X0945"
   }, {
   "primaryAgentPartyID": "FV062"
  }
]
}

I have other fields within the producerHierarchy loop besides the primaryAgentPartyID field; I just removed them for brevity.

Read By: JsonPath
Loop Jsonpath query: "$.producerHierarchy[*]"

Mapping:
Column                         Json query

primaryAgentPartyID    "primaryAgentPartyID"

agentPartyID                 "../agentPartyID"

 

When I run the job; and the step executes; and I review the LogRow; it successfully finds all the primaryAgentPartyIDs; but the agentPartyID is blank.  

Are you not allowed to recurse up the jsonpath chain above the loop field?

I've tried several jsonpath formats on the agentPartyID field; trying to get it included; but no luck.  I would like my output to look like this:

|=-----------+--------------+-----
|primaryAgentPartyID|agentPartyID|
|=-----------+--------------+-----
|XLB92              |XLB91       |
|SM001              |XLB91       |
|BBBBBBBBBB         |XLB91       |
|X0559              |XLB91       |
|X0945              |XLB91       |
|FV062              |XLB91       |
'------------+--------------+-----

I feel like this should be simple; but I'm not seeing it.

 

 

Community Manager

Re: tExtractJsonFields - accessing a sibling field at the same level as the loop

Change your component to use XPath instead of JSONPath and use the following XPath config...

 

Loop : "/producerHierarchy"

Mapping:
Column                         XPath query

primaryAgentPartyID   "./primaryAgentPartyID"

agentPartyID                 "../agentPartyID"

 

This will do exactly what you want.

Six Stars

Re: tExtractJsonFields - accessing a sibling field at the same level as the loop

Thanks
Didn't realize you could you xpath against a json input. Nice trick to remember.
Six Stars

Re: tExtractJsonFields - accessing a sibling field at the same level as the loop

Can this be done using Read by : JsonPath rather than Xpath? The issue that I am facing is in my output one of the element is named as "mime_type$string" this is causing the job to break and I get following error "Error on line 28 of document  : Element type "mime_type" must be followed by either attribute specifications, ">" or "/>". Nested exception: Element type "mime_type" must be followed by either attribute specifications, ">" or "/>"."

15TH OCTOBER, COUNTY HALL, LONDON

Join us at the Community Lounge.

Register Now

2019 GARNER MAGIC QUADRANT FOR DATA INTEGRATION TOOL

Talend named a Leader.

Get your copy

OPEN STUDIO FOR DATA INTEGRATION

Kickstart your first data integration and ETL projects.

Download now

What’s New for Talend Summer ’19

Watch the recorded webinar!

Watch Now

Best Practices for Using Context Variables with Talend – Part 4

Pick up some tips and tricks with Context Variables

Blog

How Media Organizations Achieved Success with Data Integration

Learn how media organizations have achieved success with Data Integration

Read

Downloads and Trials

Test drive Talend's enterprise products.

Downloads