-1

I need assistance in splitting a json array in my payload. The value i need to split is "coordinates": [25.12345672910156, -97.66523709893123] into "coordinate1": 25.12345672910156 and "coordinate2": -97.66523709893123.

my payload

{
    "reporting_user": null,
    "mission_type": null,
    "id": 171950377,
    "campaign_id": 43524,
    "place_id": 542543,
    "user_id": 23654366,
    "state": "completed",
    "created_at": "2023-03-28T11:52:01.501-04:00",
    "updated_at": "2023-03-28T11:52:34.154-04:00",
    "expires_at": "2023-04-07T11:52:00.000-04:00",
    "completed_at": "2023-03-28T11:52:33.000-04:00",
    "coordinates": [
        25.12345672910156,
        -97.66523709893123
    ],
    "started_at": "2023-03-28T11:52:00.000-04:00",
    "location_timestamp": null,
    "location_accuracy": null,
    "completed_coordinates": [
        25.12345672910156,
        -97.66522805987456
    ],
    "my_gsc_completed": null,
    "distance_to_place": "0.0123454913380641"
}

i've tried the following:

%dw 2.0
output application/json
fun nameSplit(name) = name splitBy  ,
---
payload.data map {

    "miles": $.reporting_user,
    "mission_type": $.mission_type,
    "id": $.id,
    "campaign_id": $.campaign_id,
    "place_id": $.place_id,
    "user_id": $.user_id,
    "state": $.state,
    "created_at": $.created_at,
    "updated_at": $.updated_at,
    "expires_at": $.expires_at,
    "completed_at": $.completed_at,
    "coordinates1": nameSplit($.coordinates)[0],
    "coordinates2": nameSplit($.coordinates)[1],
    "started_at": $.started_at,
    "location_timestamp": $.location_timestamp,
    "location_accuracy": $.location_accuracy,
    "completed_coordinates": $.completed_coordinates,
    "my_gsc_completed": $.my_gsc_completed,
    "distance_to_place": $.distance_to_place
}
  • The script has a syntax error and its input is expected to be an array. Please fix these in the question. – aled Jul 21 '23 at 18:51

2 Answers2

1

You should read the documentation before trying a function. You would see that splitBy() is to split a string into an array. You already have an array so you could just refer to its elements instead:

%dw 2.0
output application/java
---
{
    coordinate1: payload.coordinates[0],
    coordinate2: payload.coordinates[1]
}

Output:

{coordinate1=25.12345672910156, coordinate2=-97.66523709893123}

Also map() applies only to arrays, so it doesn't make sense to use it in a payload that is an object. And your payload doesn't has a data key so it will return null and do nothing.

Note that if you just need to get the values just reference the same expressions: payload.coordinates[0] and payload.coordinates[1]

aled
  • 21,330
  • 3
  • 27
  • 34
  • hi @aled, thanks for your response. Your solution placed both values in coordinate 1 and made coordinate 2 null { "coordinate1": [ 27.78919982910156, -97.66523709893443 ], "coordinate2": null } – ilya.fastiv Jul 21 '23 at 18:21
  • Not in my test. It worked fine when I tried my script in the [DataWeave Playground](https://dataweave.mulesoft.com/learn/) with your input. Have you changed the input maybe? – aled Jul 21 '23 at 18:36
  • hi @aled, the following gave me the correct result: %dw 2.0 output application/json --- payload map { "coordinates1": $.coordinates[0], "coordinates2": $.coordinates[1] } ------also what did you ding my question negatively? – ilya.fastiv Jul 21 '23 at 18:36
  • I also tried your solution in playground, but the answer i posted gave me the correct result in playground. why did you ding my question negatively? please explain so i don't make the same mistake. thanks – ilya.fastiv Jul 21 '23 at 18:41
  • It wasn't me. I can guess because the question is not giving enough context to understand the full problem or trying to use operations for incorrect types. I strongly recommend to read [How to Ask](https://stackoverflow.com/help/how-to-ask). – aled Jul 21 '23 at 18:47
0
%dw 2.0
output application/json
---
payload map {

    "miles": $.reporting_user,
    "mission_type": $.mission_type,
    "id": $.id,
    "campaign_id": $.campaign_id,
    "place_id": $.place_id,
    "user_id": $.user_id,
    "state": $.state,
    "created_at": $.created_at,
    "updated_at": $.updated_at,
    "expires_at": $.expires_at,
    "completed_at": $.completed_at,
    "coordinates1": $.coordinates[0],
    "coordinates2": $.coordinates[1],
    "started_at": $.started_at,
    "location_timestamp": $.location_timestamp,
    "location_accuracy": $.location_accuracy,
    "completed_coordinates": $.completed_coordinates,
    "my_gsc_completed": $.my_gsc_completed,
    "distance_to_place": $.distance_to_place
}
  • This script doesn't work for the input in the question which is not an array. It causes an error. Probably you are using an array as the input. You should amend the question. – aled Jul 21 '23 at 18:48
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 03 '23 at 05:59