Skip to main content
?

Object Key Change in Array

Overview

Create a Server that receives data in array of objects format and respond in with an NEW array object format.

In the below example, a new object is created by extracting the values from the original JSON object.

Additionally, the first name is capitalized.

Supporting Concepts

Basic concepts needed for the use case
TopicDescription
APIAn API in API AutoFlow is simply an OpenAPI model
ServerA server accepts and handles the request and response.
SimulationData simulation is a mock data simulated for the purpose of visualizing the data in every step of the workflow.
  • Simulated data is NOT the real data but a sample data you create.
  • To use real data, use the Transaction feature to capture the data you send from Postman or CURL.
ScopeA scope is a namespace for variables.
Data TypesData types describe the different types or kinds of data that you are gonna store and work with.
Use case specific concepts
TopicDescription
Action
iteration/map
Iterate over array of data mapping result to each array position.
Action
variable/set
This Action set variable.

Original JSON

[
{
"data": {
"field1": "john",
"field2": "doe"
}
},
{
"data": {
"field1": "joe",
"field2": "blow"
}
}
]

New JSON

Create a new key users and store the value in a new object.

{
"users": [
{
"user": {
"first_name": "john",
"last_name": "doe"
}
},
{
"user": {
"first_name": "joe",
"last_name": "blow"
}
}
]
}

Details

The HTTP request has an array of objects inputs:

  • data: main key that wraps the data
    • field1: key where the "first name" is stored
    • field2: key where the "last name" is stored

The goal of the operation is to create an array with NEW JSON:

  • user: main key that wraps the data
    • first_name: key where the "first name" is stored
    • last_name: key where the "last name" is stored

Content

INPUT: HTTP Request

1: Create an API endpoint

Required Concepts

Learn how to create an API.

Create an API

From the left navigation, go to the API section and create a new API.

Create API
  • ID: sample-data-transformation
Create an API Path
Create API Path
  • Path: /obj-change-key-in-array
  • Method: POST

2. Create a Server Operation

Required Concepts

Learn how to create a Server.

Create a Server

From the left navigation, go to the Server section and create a new Server.

Create Server
  • Server ID: sample-data-transformation
  • Port Number: 1114 Feel free to select your own port number
  • Linked API: sample-data-transformation (select the API you created above)
Create a Server Operation
Create Server Operation
  • Press the "Add API Operation"
  • Select the API endpoint created above

3 : Create Data Simulation using Real Data

Required Concepts

Learn how to create a Simulation.

We will use the "real data" to create the test simulation.

4. Send a HTTP request from Postman or CURL

Send Postman Request

API Autoflow Postman Collections

cURL
curl --location 'localhost:1114/obj-change-key-in-array' \
--header 'Content-Type: application/json' \
--data '[
{
"data": {
"field1": "john",
"field2": "doe"
}
},
{
"data": {
"field1": "joe",
"field2": "blow"
}
}
]'
2. Check the data is received by the server endpoint

API Autoflow captures the data received and it can be used to create data simulation.

Simulation

Action(s)

Required Concepts

Learn how to create a Actions.

Add actions to transform the data.

1 : Iteration over each value

Iteration Map
Required Concepts

Learn how to create a iteration/map.

SETTINGS
Iteration Map

ITERATION: data

[request: body]

SCOPE: string

loop-var
note

You can not access iteration scope loop-var outside the iteration.

Variable Set : Create a NEW Object
Required Concepts

Learn how to use variable/set actions.

Add Variable Set
SETTINGS

value: object

user: object

first_name: data

[loop-var: value > data > field1]

last_name: data

[loop-var: value > data > field2]

OUTPUT

variables: output

ITERATION OUTPUT

variables: output

OUTPUT: HTTP Response

1. Create a NEW object and map the IP and Subnet

Since we need to respond in a JSON object with a key users, we can create a new object in the HTTP response.

HTTP Response
SETTINGS
HTTP Output

body: object

users: object

[variables: output]

Mapping the action output to the HTTP response output
  • Data referenced in HTTP response is what gets sent back to the client.
  • Map the output from the actions to be sent back.

NOTE: By default, the action output is set to variable output. If you intend to keep each action's output without it being overwritten by the next action, simply rename the output location in the action's output.

2. Test the API with Postman or CURL

cURL

curl --location 'localhost:1114/obj-change-key-in-array' \
--header 'Content-Type: application/json' \
--data '[
{
"data": {
"field1": "john",
"field2": "doe"
}
},
{
"data": {
"field1": "joe",
"field2": "blow"
}
}
]'