how to convert a JSON data into map in Flutter? - string

my JSON data is like this:
{
notification:{
title: flutter fcm,
body: i got a firebase notification
},
data:{
myKey: myValue
}
}
how should I convert it into a map??
This is the value printed on onMessage() callback of firebase push notification. It is printing on my console. I want to convert this to map. This JSON data's runtimeType is String.

We can use the dart:convert package and the json.decode method to convert a JSON String to a Map:
import 'dart:convert';
final Map<String, dynamic> jsonData = json.decode(jsonData);

Related

NODE.js Lambda dynamodb documentClient get - returned data is not in [] JSON format. How to return a full JSON document datatype form GET

I'm new to Node.js/AWS lambda. Ive successfully created several documentClient QUERY functions that return a single or multiple item JSON Document in this format:
[
{
"name": "andy",
"color": "purple",
"snack": "oreos"
}
]
When I use documentClient GET and get back my single record its in THIS format, which is not playing well with the client code (apple / ios swift)
{
"name": "andy",
"color": "purple",
"snack": "oreos"
}
I'm hopign i can change the format returned from documentClient.get() to include the fill JSON document format including leading and trailing brackets .. []
I am a node.js & aws.lambda and documentClient novice, so apologies if this is a very basic question....
provided in above text
If I understood well, you're receiving an object instead of a array.
You can use the scan function to retrieve an array of results:
var params = {
TableName : 'Table',
FilterExpression : 'Year = :this_year',
ExpressionAttributeValues : {':this_year' : 2015}
};
var documentClient = new AWS.DynamoDB.DocumentClient();
documentClient.scan(params, function(err, data) {
if (err) console.log(err);
else console.log(data);
});
Or you can transform the result to array:
const document = await documentClient.get({
TableName: "table-of-example",
Key: {
id: "id-of-example"
}
});
return [data]
Please read the document to understand more how the document dynamodb sdk works: https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/DocumentClient.html#scan-property

How to decode encode string in nodejs

I am receiving below encoded string
"{\"type\":\"recipe\",\"id\":\"1624955566083\",\"entity\":\"loves\",\"count\":1}"
when i am doing like following it gives me undefined for each value. How can i generate JSON object from encoded string ?
const { type, id, entity, count } = JSON.parse(req.body);
console.log(type, id, entity); // All the values are undefined
I have to do JSON.parse(JSON.parse(req.body)) to make it work.

Stripe having trouble extracting the json card id

With Stripe php I am creating a new source with code below.
then when I show $response_all it looks like good json like:
{id: "card_xxxxxxxxxx", object: "card", address_city: null, address_country: null, address_line1: null, …} etc.......
however when I try to extract just the id only using
$response_id = $response.id;
then the json has a TEXT prefix like
"Stripe\Card JSON: {
"id": "card_xxxxxxxxxx", etc
Q: How Can I extract the Card id without getting the text prefix?
$stripe_customer = $_POST['stripe_customer'];
$stripe = new \Stripe\StripeClient(
'sk_test_xxxxxxxxx'
);
$response = $stripe->customers->createSource(
$stripe_customer,
['source' => $token]
);
$response_all = $response;
$response_id = $response.id;
You need to use arrow notation in PHP to get object properties:
$response_id = $response->id
. is string concatenation in PHP, which means that in your code you probably saw the whole JSON object printed with the string id added to the end.

How to put script into elasticsearch using their node client?

I'm using the latest version of elasticsearch npm with elasticsearch version 6.4 and trying to put new script.
According to thier documentation; putScript function takes id and body properties.
So when i try to call it, for instance:
client.putScript({
id: 'date_formatter',
body: {
lang: "painless",
source: `// Get each field value as string
String datetime = doc[params.field].value.toString();
// Create format object based on string
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(params.format);
// cast datetime into ZonedDateTime to use format function
ZonedDateTime zdt = ZonedDateTime.parse(datetime);
// return formatted date
return zdt.format(formatter);`
}
})
It returns { acknowledged: true } as expected, But when i check it through kibana, it returns:
{
"_id": "date_formatter",
"found": true,
"script": {
"lang": "mustache",
"source": """{"lang":"painless"}"""
}
}
Is there any way to put script into elasticsearch through node client?
You need to wrap both lang and source into a script section basically the same way as described here:
client.putScript({
id: 'date_formatter',
body: {
script: {
lang: "painless",
source: `// Get each field value as string
String datetime = doc[params.field].value.toString();
// Create format object based on string
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(params.format);
// cast datetime into ZonedDateTime to use format function
ZonedDateTime zdt = ZonedDateTime.parse(datetime);
// return formatted date
return zdt.format(formatter);`
}
}
})

how to get json rest response - node.js

How can i get JSON objects in the following case:
Here is my post request:
var articles = client.post("http://host:port/path", args, function (data, response) {
// console.log(xml.parseBuffer(data))
console.log(data.toString('utf8'));
// return xml.parseBuffer(data);
});
result of data.toString('utf8') look like:
<WebServiceResponse>
<page>
...
</page>
<articleDataList>
<articleId>
<idArticle>100000</idArticle>
<index>test</index>
</articleId>
<goodType>test</goodType>
<idAttributeSubject>100001</idAttributeSubject>
<identyfiable>false</identyfiable>
<isActive>true</isActive>
<isGoodSet>false</isGoodSet>
<longName>test</longName>
<translationSubjectId>
<idTranslationSubject>100408</idTranslationSubject>
</translationSubjectId>
<unitCode>szt</unitCode>
<vatRate>0.2300</vatRate>
</articleDataList>
<articleDataList>
...
</articleDataList>
<articleDataList>
...
and xml.parseBuffer(data) looks:
{ name: 'articleDataList', childs: [Object] },
I need put into articles objects with only: idArticle, index and shortName
Is there any easy way?
In this output:
{ name: 'articleDataList', childs: [Object] }
Object represents javascript object that you need to stringify like this: JSON.stringify(Object)
Or you should convert this whole Buffer to string first, and then convert to JSON or javascript object:
xml.parseString(data.toString('utf8'))

Resources