<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>question How to access the nested fields in a json using expression language in Nifi? in Support Questions</title>
    <link>https://community.cloudera.com/t5/Support-Questions/How-to-access-the-nested-fields-in-a-json-using-expression/m-p/369420#M240485</link>
    <description>&lt;P&gt;Hi,&lt;BR /&gt;I have an input json as below:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;[
  {
    "orderId": "1234567890",
    "orderName": "Test1",
    "orderItems": [
      {
        "orderItemId": "01",
        "orderItemName": "test1-OrderItem1"
      },
      {
        "orderItemId": "02",
        "orderItemName": "test1-OrderItem2"
      }
    ]
  },
  {
    "orderId": "12235",
    "orderName": "Test2",
    "orderItems": [
      {
        "orderItemId": "01",
        "orderItemName": "test2-OrderItem1"
      }
    ]
  },
  {
    "orderId": "12236",
    "orderName": "Test3",
    "orderItems": [
      {
        "orderItemId": "01",
        "orderItemName": "test3-OrderItem1"
      }
    ]
  }
]&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;I want to convert it into text form where we want the order details and then item details for each map in the list.&lt;BR /&gt;Here is the desired output:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;1234567890 Test1
01 test1-OrderItem1
02 test1-OrderItem2
12235 Test2
01 test2-OrderItem1
12236 Test3
01 test3-OrderItem1&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;This is json to csv conversion, and I am using the ConvertRecord processor and below are the reader and writer configured:&lt;BR /&gt;1. Record Reader - JsonTreeReader&lt;BR /&gt;2. Record Writer - FreeFormTextRecordSetWriter&lt;BR /&gt;&lt;BR /&gt;For the&amp;nbsp;FreeFormTextRecordSetWriter controller service in the text property, I added the key names from the input json like this ${keyName} and got the values but in the next line, I wanted orderItemId from all the maps in the orderItems list.&lt;BR /&gt;&lt;BR /&gt;How can I access the values from orderItems list?&lt;BR /&gt;&lt;BR /&gt;Thanks in advance!&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
    <pubDate>Wed, 26 Apr 2023 06:12:44 GMT</pubDate>
    <dc:creator>janvit04</dc:creator>
    <dc:date>2023-04-26T06:12:44Z</dc:date>
    <item>
      <title>How to access the nested fields in a json using expression language in Nifi?</title>
      <link>https://community.cloudera.com/t5/Support-Questions/How-to-access-the-nested-fields-in-a-json-using-expression/m-p/369420#M240485</link>
      <description>&lt;P&gt;Hi,&lt;BR /&gt;I have an input json as below:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;[
  {
    "orderId": "1234567890",
    "orderName": "Test1",
    "orderItems": [
      {
        "orderItemId": "01",
        "orderItemName": "test1-OrderItem1"
      },
      {
        "orderItemId": "02",
        "orderItemName": "test1-OrderItem2"
      }
    ]
  },
  {
    "orderId": "12235",
    "orderName": "Test2",
    "orderItems": [
      {
        "orderItemId": "01",
        "orderItemName": "test2-OrderItem1"
      }
    ]
  },
  {
    "orderId": "12236",
    "orderName": "Test3",
    "orderItems": [
      {
        "orderItemId": "01",
        "orderItemName": "test3-OrderItem1"
      }
    ]
  }
]&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;I want to convert it into text form where we want the order details and then item details for each map in the list.&lt;BR /&gt;Here is the desired output:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;1234567890 Test1
01 test1-OrderItem1
02 test1-OrderItem2
12235 Test2
01 test2-OrderItem1
12236 Test3
01 test3-OrderItem1&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;This is json to csv conversion, and I am using the ConvertRecord processor and below are the reader and writer configured:&lt;BR /&gt;1. Record Reader - JsonTreeReader&lt;BR /&gt;2. Record Writer - FreeFormTextRecordSetWriter&lt;BR /&gt;&lt;BR /&gt;For the&amp;nbsp;FreeFormTextRecordSetWriter controller service in the text property, I added the key names from the input json like this ${keyName} and got the values but in the next line, I wanted orderItemId from all the maps in the orderItems list.&lt;BR /&gt;&lt;BR /&gt;How can I access the values from orderItems list?&lt;BR /&gt;&lt;BR /&gt;Thanks in advance!&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 26 Apr 2023 06:12:44 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Support-Questions/How-to-access-the-nested-fields-in-a-json-using-expression/m-p/369420#M240485</guid>
      <dc:creator>janvit04</dc:creator>
      <dc:date>2023-04-26T06:12:44Z</dc:date>
    </item>
    <item>
      <title>Re: How to access the nested fields in a json using expression language in Nifi?</title>
      <link>https://community.cloudera.com/t5/Support-Questions/How-to-access-the-nested-fields-in-a-json-using-expression/m-p/376424#M242909</link>
      <description>&lt;P&gt;I would do this with a Groovy based InvokeScriptedProcessor&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="joseomjr_0-1694907126587.png" style="width: 400px;"&gt;&lt;img src="https://community.cloudera.com/t5/image/serverpage/image-id/38467iBC18DBE7C9330674/image-size/medium?v=v2&amp;amp;px=400" role="button" title="joseomjr_0-1694907126587.png" alt="joseomjr_0-1694907126587.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Using this code:&lt;/P&gt;&lt;LI-CODE lang="java"&gt;import groovy.json.JsonOutput
import groovy.json.JsonSlurper
import java.nio.charset.StandardCharsets
import org.apache.commons.io.IOUtils

class GroovyProcessor implements Processor {
    PropertyDescriptor BATCH_SIZE = new PropertyDescriptor.Builder()
        .name("BATCH_SIZE")
        .displayName("Batch Size")
        .description("The number of incoming FlowFiles to process in a single execution of this processor.")
        .required(true)
        .defaultValue("1000")
        .addValidator(StandardValidators.POSITIVE_INTEGER_VALIDATOR)
        .build()

    Relationship REL_SUCCESS = new Relationship.Builder()
        .name("success")
        .description('FlowFiles that were successfully processed are routed here')
        .build()

    Relationship REL_FAILURE = new Relationship.Builder()
        .name("failure")
        .description('FlowFiles that were not successfully processed are routed here')
        .build()
    
    ComponentLog log 
    
    void initialize(ProcessorInitializationContext context) { log = context.logger }
    Set&amp;lt;Relationship&amp;gt; getRelationships() { return [REL_FAILURE, REL_SUCCESS] as Set }
    Collection&amp;lt;ValidationResult&amp;gt; validate(ValidationContext context) { null }
    PropertyDescriptor getPropertyDescriptor(String name) { null }
    void onPropertyModified(PropertyDescriptor descriptor, String oldValue, String newValue) { }
    List&amp;lt;PropertyDescriptor&amp;gt; getPropertyDescriptors() { Collections.unmodifiableList([BATCH_SIZE]) as List&amp;lt;PropertyDescriptor&amp;gt; }
    String getIdentifier() { null }

    JsonSlurper jsonSlurper = new JsonSlurper()
    JsonOutput jsonOutput = new JsonOutput()
    
    void onTrigger(ProcessContext context, ProcessSessionFactory sessionFactory) throws ProcessException {
        ProcessSession session = sessionFactory.createSession()
        try {
            List&amp;lt;FlowFile&amp;gt; flowFiles = session.get(context.getProperty(BATCH_SIZE).asInteger())
            if (!flowFiles) return

            flowFiles.each { flowFile -&amp;gt;
                List data = null
                session.read(flowFile, { 
                    inputStream -&amp;gt; data = jsonSlurper.parseText(IOUtils.toString(inputStream, StandardCharsets.UTF_8)) 
                } as InputStreamCallback)
                List outputData = []
                data.each { order -&amp;gt;
                    outputData.add("${order.orderId} ${order.orderName}")
                    order.orderItems.each { orderItem -&amp;gt;
                        outputData.add("${orderItem.orderItemId} ${orderItem.orderItemName}")
                    }
                }
                FlowFile newFlowFile = session.create()
                newFlowFile = session.write(newFlowFile, { outputStream -&amp;gt; outputStream.write(outputData.join('\n').getBytes(StandardCharsets.UTF_8)) } as OutputStreamCallback)
                session.transfer(newFlowFile, REL_SUCCESS)
                session.remove(flowFile)
            }
            session.commit()
        } catch (final Throwable t) {
            log.error('{} failed to process due to {}; rolling back session', [this, t] as Object[])
            session.rollback(true)
            throw t
        }
    }
}

processor = new GroovyProcessor()&lt;/LI-CODE&gt;&lt;P&gt;Don't let all that code scare you when the part that's doing the formatting is only these lines:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="joseomjr_1-1694907251400.png" style="width: 400px;"&gt;&lt;img src="https://community.cloudera.com/t5/image/serverpage/image-id/38468i535CE6C5B08C9DA9/image-size/medium?v=v2&amp;amp;px=400" role="button" title="joseomjr_1-1694907251400.png" alt="joseomjr_1-1694907251400.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;This is the generated output:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="joseomjr_2-1694907283061.png" style="width: 400px;"&gt;&lt;img src="https://community.cloudera.com/t5/image/serverpage/image-id/38469iA7614604AF36AB1E/image-size/medium?v=v2&amp;amp;px=400" role="button" title="joseomjr_2-1694907283061.png" alt="joseomjr_2-1694907283061.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 16 Sep 2023 23:35:03 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Support-Questions/How-to-access-the-nested-fields-in-a-json-using-expression/m-p/376424#M242909</guid>
      <dc:creator>joseomjr</dc:creator>
      <dc:date>2023-09-16T23:35:03Z</dc:date>
    </item>
  </channel>
</rss>

