jsonDOMGet

Version 23.4.8841


jsonDOMGet


Get values from a JSON document.

必要なパラメータ

  • map:*: A set of one or more inputs where the name of the mapped parameter proceeds the colon (for example, map:foo), and the value is the jsonpath to the desired json element in the document (see Examples for details).

オプションのパラメータ

  • uri: A JSON file URI. This can be a dynamic file reference such as [FilePath] for the input file, a static filepath on disk, or a public URL for a JSON file.
  • text: A readable handle reference to the JSON data. This handle is created by the jsonOpen operation and is helpful when the input JSON is not a file or raw text, or when it is necessary to perform multiple actions that access the file data of the document.
  • handle: A readable handle reference to the JSON data. This handle is created by the jsonOpen operation and is only necessary when the target JSON is not an input file. See jsonOpen for details and examples.
  • repeatelement#: The jsonpath to an element that is repeated in the document.
  • repeatmode: Defines the way in which the repeatelement# values are stored on the output item. The allowed values are ITEM and ARRAY. When set to ITEM (the default), the values found from the repeat element are stored as individual attributes on each output item the operation produces. When set to ARRAY, the values found from the repeat element are stored as array attributes on a single output item and need to be accessed via their 1-based index (for example, [result.color#2] would refer to the second value in the color attribute of the result item).

アウトプット属性

  • *: The output values from the JSON elements that were mapped using the map parameter. These are accessed by referencing the output item and the name of the mapped input item (for example, [results.foo]). The name of the output item (results in this case) is determined by the user. See Examples for additional context.

Retrieve Values from a JSON Document

Consider the JSON data below, which is passed as input data to a Script connector in CData Arc:

{
    "name": "Nancy",
    "age": "31",
    "gender": "Female"
}

In the Script connector, you can invoke jsonDOMGet in ArcScript to retrieve the name and age of the person defined in this data and add them as headers to a message in Arc. The ArcScript code below shows an example of how to do this:

<!-- Setting the input uri and map parameters -->
<arc:set attr="json.uri" value="[FilePath]" />
<arc:set attr="json.map:name" value="/json/name" />
<arc:set attr="json.map:age" value="/json/age" />

<!-- Calling the operation, passing in the json item and creating a "result" output item -->
<arc:call op="jsonDOMGet" in="json" out="result">
  <!-- result.name and result.age has the value of the json path because map:name and map:age was 
       set in the input to the op, and result is the name of the output item. Once this
       op closes, result falls out of scope, so use your value inside the arc:call block --> 
  <!-- Setting the values for name and age as headers on the message -->
  <arc:set attr="output.header:personsname" value="[result.name]" />
  <arc:set attr="output.header:personsage" value="[result.age]" />
</arc:call>

<!-- Setting the output file and pushing the file out -->
<arc:set attr="output.filepath" value="[FilePath]" />
<arc:push item="output" />

After this code executes, Arc pushes the file with the newly added headers as output down the flow.

Access a Specific Index of an Array in a JSON Object

Users often need to access a specific index of an array in JSON data. For example, you might want to access the id of the second order object in the orders array in the JSON object below, which is passed as an input file to a Script connector.

{
    "warehouseid":"WH1234",
    "orders": [
        {
            "date": "02/27/2023",
            "id": "1234"
        },
        {
            "date": "02/28/2023",
            "id": "5678"
        },
        {
            "date": "03/01/2023",
            "id": "9876"
        }
    ]
}

In the Script connector, you can invoke jsonDOMGet in ArcScript to access the id of a specific order using the 1-based index jsonpath to an element present in one of the objects in the orders array. The ArcScript code below shows an example of how to do this:

<!-- Setting the input uri and map parameters -->
<arc:set attr="json.uri" value="[FilePath]" />
<arc:set attr="json.map:id" value="/json/orders/\[2\]/id" />

<!-- Calling the operation, passing in the json item and creating a "result" output item -->
<arc:call op="jsonDOMGet" in="json" out="result">
  <!-- Setting the result from the mapped id parameter as a header on the message -->
  <arc:set attr="output.header:orderid" value="[result.id]" />
</rc:call>

<!-- Setting the output file and pushing the file out -->
<arc:set attr="output.filepath" value="[FilePath]" />
<arc:push item="output" />

Use the repeatelement Parameter

Consider the JSON data below, which is passed as an input file to a Script Connector:

{
    "hello": "world",
    "colors": [
        {
            "color": "yellow",
            "example": "banana"
        },
        {
            "color": "red",
            "example": "apple"
        },
        {
            "color": "orange",
            "example": "orange"
        }
    ]
}

Inside the Script Connector, the following code is executed against the input file. The repeatelement# parameter is used to define the element that has repeating values. In this example the color and example values are repeated in the /json/colors element, which is a JSON array. When you set repeatelement#, the jsonDOMGet operation loops over the values that are present in that element.

The repeatmode parameter defines the way in which the repeatelement# values are stored as output. When set to ITEM (the default), the values found by the repeat element are stored as individual attributes on the output item. When set to ARRAY, the values found by the repeat element are stored as array attributes on the output item and need to be accessed via their 1-based index (for example, [result.color#2] would resolve to red and result.example#2 would resolve to apple).

<!-- Initializing the output data -->
<arc:set attr="out.data" />
<!-- Setting the input uri, repeatmode and map parameters -->
<arc:set attr="json.uri" value="[FilePath]" />
<arc:set attr="json.repeatelement#" value="/json/colors/" />
<arc:set attr="json.repeatmode" value="ITEM" />
<!-- Note, the values for the map are relative to the repeatelement path -->
<arc:set attr="json.map:color" value="color" />
<arc:set attr="json.map:example" value="example" /> 

<!-- Calling the operation, passing in the json item and creating a "result" output item -->
<arc:call op="jsonDOMGet" in="json" out="result">
  <!-- Setting the output data to the enumerated results of the call -->
  <arc:set attr="out.data">[out.data]
    <arc:enum list="color,example" separator=",">[_value]: [result.[_value]]\n
    </arc:enum>
  </arc:set>
</arc:call>

<!-- Setting the output filename and pushing the file out -->
<arc:set attr="out.filename" value="data.txt" />
<arc:push item="out" />

Output file:

color: yellow
example: banana
color: red
example: apple
color: orange
example: orange