Using mule expression to point to elements in JsonData Object - json

I have an inbound payload in JSON format. I'm converting it using the "JSON to Object" converter, and then passing on the data to a component (as a JsonData object.) My component then returns the same JsonData object with modifications. I'm trying to use the Amazon S3 component as the next step in my flow, and trying to tie the bucket name and other values to elements accessible in the JsonData object.
Here is the expression for the bucket name for instance:
#[json: TopKey/BucketName]
From experience this has worked with JSON.
However when I run this, here is what I get:
Message : Failed to invoke getObjectContent. Message payload is of type: JsonData
Code : MULE_ERROR-29999
Failed to invoke getObjectContent. Message payload is of type: JsonData (org.mule.api.MessagingException)
org.mule.module.s3.processors.GetObjectContentMessageProcessor:177 (http://www.mulesoft.org/docs/site/current3/apidocs/org/mule/api/MessagingException.html)
Is there a way I can use my JsonData object and pull information from it, or do I have to convert it back to something else before passing it on to the Amazon S3 component?
Thanks,

After trying a little more to play with my expression, I figured out I can just access elements the way I do it in my Java component already:
#[payload.get("TopKey").get("BucketName").getTextValue()]
and I have my BucketName!

Remove the empty space from your expression: #[json:TopKey/BucketName]

You can set the "Return Class" to java.util.Map in the "JSON to Object" processor, you can then access the value via #[payload.TopKey.BucketName]

Related

Elm Json Decode - do nothing, get original string back

I'm using the-sett/elm-aws-core to get information from the AWS API, which unfortunately is very very inconsistent. Most of the endpoints return JSON and that works fine with that lib, which takes a JSON Decoder to make a request, but the EC2 endpoint returns XML (because why not).
The lib doesn't have any options not to decode JSON as far as I can tell, which does not work at all :
let ec2 region = Service.defineRegional "ec2" "2016-11-15" Service.QUERY Service.SignV4 (Service.setXmlNamespace "https://ec2.amazonaws.com/doc/2016-11-15/") region in
let params = [("Action", "DescribeImages"), ("Version", "2016-11-15"), ("Owner.1", "self")] in
Http.request "DescribeImages" GET "/" Http.emptyBody JSONDECODERHERE |> Http.addQuery params |> Http.send (ec2 region) creds |> Task.attempt msg
Failed : Problem with the given value:
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<DescribeImagesResponse .......
As you can see in there you need to pass a JSON Decoder to Http.request, but that, of course, fails when receiving XML. Is there a way to build a "fake" JSON decoder that would actually do nothing and just pass on the raw string? I tried using Decode. string but that's still actually decoding it, which fails. If there is a way I could then run an XML decoder manually on it in my update function, which would be fine.
Thank you
It's not possible to make a "fake" decoder that does what you want, because the problem isn't with the decoding. The problem is with the parsing, which is done before decoding. Parsing is the process of converting the string into a data structure typically called an Abstract Syntax Tree (AST), but since Elm compiles to JavaScript and JSON is also a subset of JavaScript the parse result of is really just a JavaScript object. Decoding is the process of turning that untyped data structure into a properly typed data structure.
It is therefore not possible to accomplish what you want with this API. Most likely you'll need to build the http request yourself and use elm/http directly.

How to get access token with JMeter JSON Extractor and use it?

I'm trying to extract access token from the body response and use it in Header Manager for authorization.
The response of the first request is
Response
Then I use regular expression for extracting the token
Json Extractor
Then I enter a variable into the Header Manager
Header Manager
But when I run the script I receive an error:
Listener
Also, I get an error:
2019-02-09 23:45:57,822 ERROR o.a.j.e.j.j.JSONPostProcessor: Error processing JSON content in JSON Extractor, message: Use bracket notion ['my prop'] if your property contains blank characters. position: 2
Error
I assume that json path is incorrect
I have already researched a lot of questions here but they didn't help me
What is wrong in my actions?
Thanks for the response in advance!
Use JSON Path Expressions as $.access_token
You're using a Regular Expression inside a JSON Extractor, that's your problem.
You must use JSON Path expression instead.
So correct value for JSON Path Expressions is:
$..access_token
Since you have in "Names for created variables" token, you can then use it with:
${token}

Reading property from JSON object in Javascript with colons

I have a JSON object which I can console log out fine. However whenever I try to read the properties of this object I am getting "Unexpected token :" error. The json object does have a colon in it. How do i escape that colon and console.log the individual properties of the object. See that attached image for info
I think you can try out JSON serializer to get the JSON value. You can't get the JSON value without serializing it. So To serialize, you can use JsonConvert.SerializeObject("Json"). Check it out this link below for JSON serialization.
https://www.newtonsoft.com/json/help/html/SerializingJSON.htm
For serializing the JSON you should install Newtonsoft JSON package in your visual studio.

What's the proper way to extract data from a google protobuff object?

I have never encountered this sort of collection or object before until now (its the response from a request to Google-Cloud-Vision API).
I wrote a class that uses the API and does what I want correctly. However the only way that I can extract/manipulate data in the response is by using this module:
from google.protobuf.json_format import MessageToJson
I basically serialized the protobuff into a string and then used regex to get the data that I want.
There MUST be a better way than this. Any suggestions? I was hoping to have the API response give me a json dict or json dict of dicts etc... All I could come up with was turning the response into a string though.
Here is the file from the github repository:
image_analyzer.py
Thank you all in advance.
The built in json module will parse the string into a dictionary, like json.loads(MessageToJson(response1)).
You can just access the fields in the message object directly, e.g.:
response1 = vision_client.face_detection(image=image)
print(response1)
print(response1.face_annotations[0].detection_confidence)

What is the recommended way to parse JSON on Intershop?

In my pipelet I make a REST call to a 3rd party system and get back a String containing the JSON response. From the JSON I want to parse 1 attribute. What is the best way to parse JSON on Intershop Commerce Management 7.8?
Intershop includes the Jackson library.
You can use it without even need to map the whole response to a well-defined object, but you can parse it "on the fly".
See "Jackson JSON – Read Specific JSON Key" paragraph here: http://www.journaldev.com/2324/jackson-json-java-parser-api-example-tutorial
Jackson is definitely recommended, but for very simple cases you could alternatively use org.json.
Example:
JSONObject obj = new JSONObject(responseAsStr);
String accessToken = obj.getString("access_token");
However, this library is not included by default. You have to include it in build.gradle of your cartridge, e.g.:
compile group: 'org.json', name: 'json', version: '20090211'