TIBCOSoftware / flogo

Project Flogo is an open source ecosystem of opinionated event-driven capabilities to simplify building efficient & modern serverless functions, microservices & edge apps.
http://flogo.io
BSD 3-Clause "New" or "Revised" License
2.41k stars 288 forks source link

Get Data from a String JSON #518

Closed akpuvvada closed 4 years ago

akpuvvada commented 4 years ago

What is your question? Trying to get data from AWS Lambda Trigger. What ever JSON I pass to API or SQS is coming as string as given below. I want to extract the data from that JSON - in this case zipcode value 07604.

"body": "{\"zipcode\":\"07604\"}",

I tried doing below. Pass input from trigger to flow as

"input": {
              "CityCode_Input": "=$.event.data",

CityCode_Input is defined as object.

Then I tried to get the data as $flow.CityCode_Input.zipcode. Re;avant code snippet below.

{
            "id": "rest_3",
            "name": "AccuWeather_GET_RESTInvoke",
            "description": "Invokes a REST Service",
            "activity": {
              "ref": "#rest",
              "input": {
                "pathParams": {
                  "mapping": {
                    "zipC": "=$flow.CityCode_Input.zipcode"
                  }
                },

Please tell us about your environment (Operating system, docker version, browser & web ui version, etc): AWS Lambda

Flogo version (CLI & contrib/lib. If unknown, leave empty or state unknown): 0.X.X Latest Version

fm-tibco commented 4 years ago

What type is ‘CityCode_Input’ set to on the flow? Also if you log ‘CityCode_Input’ what does it print?

akpuvvada commented 4 years ago

It is defined as Object

"metadata": {
          "input": [
            {
              "name": "CityCode_Input",
              "type": "object"
            },
akpuvvada commented 4 years ago

Please help!

fm-tibco commented 4 years ago

if you log ‘CityCode_Input’ what does it print? Also, do you get an error? Do you have the logs for this?

akpuvvada commented 4 years ago

Log activity is not working in AWS. I am checking on that on https://github.com/project-flogo/aws-contrib/issues/10.

However, from what I understand it is coming as String. Like -> {"body":"{\"zipcode\":\"07604\"}"}

fm-tibco commented 4 years ago

Once the logging activity is fixed, try logging it. Also, have you tried the following?

"id": "rest_3",
"name": "AccuWeather_GET_RESTInvoke",
"description": "Invokes a REST Service",
"activity": {
  "ref": "#rest",
  "input": {
    "pathParams": {
      "mapping": {
        "zipC": "=$flow.CityCode_Input.body.zipcode"
      }
    },
akpuvvada commented 4 years ago

Ok, I got the log to work now, thanks to Samip.

I am getting null for that.

Below is the complete log if I log '$.event' of which I am mapping 'data' to 'CityCode_Input' as mentioned in issue description.

image

Hope these details help. Let me know if you need more details.

{
    "body": "{\"zipcode\":\"07604\"}",
    "headers": {
        "Accept": "*/*",
        "Accept-Encoding": "gzip, deflate",
        "Cache-Control": "no-cache",
        "Content-Type": "application/json",
        "Host": "ob2nu3js53.execute-api.us-east-2.amazonaws.com",
        "Postman-Token": "98fba720-0650-412b-9e66-0780cea47fe5",
        "User-Agent": "PostmanRuntime/7.20.1",
        "X-Amzn-Trace-Id": "Root=1-5de6c75a-e499295e165fdd973afafa77",
        "X-Forwarded-For": "139.180.244.174",
        "X-Forwarded-Port": "443",
        "X-Forwarded-Proto": "https"
    },
    "httpMethod": "POST",
    "isBase64Encoded": false,
    "multiValueHeaders": {
        "Accept": [
            "*/*"
        ],
        "Accept-Encoding": [
            "gzip, deflate"
        ],
        "Cache-Control": [
            "no-cache"
        ],
        "Content-Type": [
            "application/json"
        ],
        "Host": [
            "ob2nu3js53.execute-api.us-east-2.amazonaws.com"
        ],
        "Postman-Token": [
            "98fba720-0650-412b-9e66-0780cea47fe5"
        ],
        "User-Agent": [
            "PostmanRuntime/7.20.1"
        ],
        "X-Amzn-Trace-Id": [
            "Root=1-5de6c75a-e499295e165fdd973afafa77"
        ],
        "X-Forwarded-For": [
            "139.180.244.174"
        ],
        "X-Forwarded-Port": [
            "443"
        ],
        "X-Forwarded-Proto": [
            "https"
        ]
    },
    "multiValueQueryStringParameters": null,
    "path": "/AWS_Lambda_REST_POC_INI2",
    "pathParameters": null,
    "queryStringParameters": null,
    "requestContext": {
        "accountId": "034413604559",
        "apiId": "ob2nu3js53",
        "domainName": "ob2nu3js53.execute-api.us-east-2.amazonaws.com",
        "domainPrefix": "ob2nu3js53",
        "extendedRequestId": "EJQWOFSZiYcFcZA=",
        "httpMethod": "POST",
        "identity": {
            "accessKey": null,
            "accountId": null,
            "caller": null,
            "cognitoAuthenticationProvider": null,
            "cognitoAuthenticationType": null,
            "cognitoIdentityId": null,
            "cognitoIdentityPoolId": null,
            "principalOrgId": null,
            "sourceIp": "139.180.244.174",
            "user": null,
            "userAgent": "PostmanRuntime/7.20.1",
            "userArn": null
        },
        "path": "/default/AWS_Lambda_REST_POC_INI2",
        "protocol": "HTTP/1.1",
        "requestId": "3e0cfe92-f221-44e1-becd-cc0d23bb77ce",
        "requestTime": "03/Dec/2019:20:36:42 +0000",
        "requestTimeEpoch": 1575405402973,
        "resourceId": "md77fi",
        "resourcePath": "/AWS_Lambda_REST_POC_INI2",
        "stage": "default"
    },
    "resource": "/AWS_Lambda_REST_POC_INI2",
    "stageVariables": null
}
akpuvvada commented 4 years ago

Please help!

skothari-tibco commented 4 years ago

So are you trying to access "zipcode" from the JSON given above ? You said when you log $.event you get the above one. To access zipCode from above event you'll need to do $.event.body[\"zipcode\"] or $.event.body.zipcode .

akpuvvada commented 4 years ago

Hi Samip, I already tried that and it did not work. In-affect, even in the above code, I am trying the same. I am passing data into flow as 'CityCode_Input' and then trying to access 'zipcode' from that as $flow.CityCode_Input.zipcode

skothari-tibco commented 4 years ago

Can you do a small thing ? Can you output $flow.CityCode_Input.body or just try accessing "body" part of the data. Not the 'ZipCode'. I suspect that the body should output "{\"zipcode\":\"07604\"}". If it does; that means it's a string and not an object. Hence you need to use "string" functions to get the code '07604'(like splitting and trimming). 'https://github.com/project-flogo/contrib/tree/master/function/string'

lixingwang commented 4 years ago

@akpuvvada , Samip is right the body is string not an object so to get the zipcode value you can use JSON path function.

Please try this json.path("$.zipcode", coerce.toObject($flow.CityCode_Input.body))

akpuvvada commented 4 years ago

How do I map it in an activity input?

image

lixingwang commented 4 years ago

Add = before json.path image

akpuvvada commented 4 years ago

Getting below error:

2019-12-04T20:42:51.942Z    ERROR   [flogo.activity.log] -  Execution failed for Activity[log_2] in Flow[Lambda_GET_WeatherLimit_POC] - key error: zipcode not found in object
github.com/project-flogo/core/support/log.(*zapLoggerImpl).Errorf
lixingwang commented 4 years ago

@akpuvvada Can you share your app.json?

akpuvvada commented 4 years ago

Hi Li, Sorry. My mappings were wrong. I updated it now and it is working. Thanks for the help. json.path and coerce helped.