How to read JSON file data and convert to OBJECT using MUELSOFT - anypoint-studio

I am new to this Muelsoft - AnyPoint Studio.
Requirement is
Reading JSON file data and converting into OBJECT.

Use json to object transformer (payload transformation) which
transforms the json data to object as expected.
Use the link to tranform the payload to different representation https://docs.mulesoft.com/mule-user-guide/v/3.7/dataweave-examples

Related

RabbitMQ - Send a JSON message

I send the following message with content type application/json:
However whene i get messages from the same RabbitMQ Web console, it shows the payload as String.
What am I doing wrong? Or am I fundamentally misunderstanding and the Payload is always of type String?
From the official docs:
AMQP messages also have a payload (the data that they carry), which AMQP brokers treat as an opaque byte array. The broker will not inspect or modify the payload. It is possible for messages to contain only attributes and no payload. It is common to use serialisation formats like JSON, Thrift, Protocol Buffers and MessagePack to serialize structured data in order to publish it as the message payload. AMQP peers typically use the "content-type" and "content-encoding" fields to communicate this information, but this is by convention only.
So basically, RabbitMQ has no knowledge on JSON, messages all are just byte arrays to it
From NodeJS Context:
If we want to send JSON object as message, we may get the following error:
The first argument must be of type string or an instance of Buffer,
ArrayBuffer, or Array or an Array-like Object. Received an instance of
Object
So, we can convert the JSON payload as string and parse it in the worker. We stringify the JSON object before sending the data the Queue-
let payloadAsString = JSON.stringify(payload);
And from worker's end, we can then JSON.parse
let payload = JSON.parse(msg.content.toString());
//then access the object as we normally do, i.e. :
let id = payload.id;
For anyone using .Net to send objects via RabbitMQ.
You have to serialise your JSON object to byte array, send via RabbitMQ then de-serialise after receiving. You can do this like this:
Install the Newtonsoft JSON library
using Newtonsoft.Json;
Create a model for your JSON object message (in this case AccountMessage)
Serialise your object into byte array like this:
byte[] messagebuffer = Encoding.Default.GetBytes(JsonConvert.SerializeObject(accountMessage) );
After receiving the message data, you can de-serialise like this:
AccountMessage receivedMessage = JsonConvert.DeserializeObject<AccountMessage>(Encoding.UTF8.GetString(body));
from here
Content Type and Encoding
The content (MIME media) type and content encoding fields allow publishers communicate how message payload should be deserialized and decoded by consumers.
RabbitMQ does not validate or use these fields, it exists for applications and plugins to use and interpret.
by the way, using the rabbitMQ web gui, you use the words content_type, however in code (javascript confirmed), you use the key name contentType. it's a subtle difference, but enough to drive you crazy.

Python ast.literal_eval on dictionary string not working (SyntaxError: invalid syntax)

I am trying to process a dataset with JSON data. However, the data have been written on a file without being parsed. That means that a python dictionary is written in the file as a string instead of a JSON object as a string.
I've found a module (AST) that will do the job to convert the string to a dictionary again using the ast.literal_eval function.
However, I am getting a very strange error in some of the instances:
The code reads from a text file and apply the following to each line:
ast.literal_eval(line.rstrip())
It seems some of the characters are not ok with the AST module.
Need to recall as well that this is not happening with all the dataset, just with some instances.
Any ideas?
Many thanks in advance.
Try exploring the json package. It is cleaner and more standard way of converting strings to dictionary
json.loads(inputStr) // Converts string -> dict
json.dumps(inputJson) // Converts dict -> string
Hope this helps. Cheers!

Noflo .fbp array initiallizer

I'm using noflo and am trying to send an array as an initiallizer. There doesn't seem to be a supported (or at least documented) way to do this.
I'm currently using:
'["Kicker"]' -> IN Nodes(strings/ParseJson)
'{"in":"go!"}' -> IN Config(strings/ParseJson)
Nodes() OUT -> NODES MyComponent(noflotest/Universe)
Config OUT -> CONFIG MyComponent()
Is there a better way to do this?
Currently arrays and other complicated data structures are not supported in the .fbp syntax. There is a feature request about this.
Right now you have three options:
If FBP parser accepts your string (see the matching rules), you can first send it to the strings/ParseJson component to turn it to the appropriate data structure
Reading the value from a JSON or YAML file and passing it through the appropriate parser component
Converting your graph to the JSON graph format

Modifying ServiceStack's JSON output

I have to build a REST service with ServiceStack; the responses must have a certain format. Both JSON and XML are to be supported. The standard serializers do not return the response in the format I need.
For JSON, it would be enough to wrap the result, e.g. if a function returns a list of Site objects, the JSON serializer gives me [{...}, ...], but I need {"Sites": [{...}, ...]}. The requested content-type would be "Sites+json" in this case. For other functions, "Sites" would be replaced by something else.
How can I achieve this?
Edit:
The XML has to be the direct "translation" of the JSON, like
<Sites>...</Sites> instead of {"Sites":...}.
The standard XML serialization works differently, it always puts in the data type as well.
Has anyone an idea how to do this? I guess I have to write my own XML serializer and map all my XML types (like Sites+xml,...) to it?

Writing simple dictionary using java & google dictionary api

http://www.google.com/dictionary/json?callback=dict_api.callbacks.id100&q=test&sl=en&tl=en&restrict=pr%2Cde&client=te
(replace test with your favorite keyword)
using this i want to write simple dictionary....
problem - api give json output how i get it using java?
Simple Parsing
Per default, JSON will just produce a nested graph of maps and lists. Here is an example of default parsing:
import org.svenson.JSONParser;
// assume json to be a JSON datasets as String
Object o = JSONParser.defaultJSONParser().parse(json);
o will now contain either a map or a list instance, depending on whether the first symbol in the JSON is '{' or '['.