Here with javascript we do an API POST using a external JSON file. Then we have 2 files order.json and app.js
my server:
ubuntu 22.04
node v19.2.0
npm 8.19.3
The script get data from the file order.js using readFile.
app.js
var myHeaders = new Headers();
myHeaders.append("api-key", "123");
myHeaders.append("Content-Type", "text/plain");
myHeaders.append("Cookie", "session_id=123");
// get order.json file
var fs = require('fs')
fs.readFile('order.json', 'utf8', (err, data) => {
if (err) throw err
let orders = JSON.parse(data)
let order_line = orders.map(o => ([0, 0, {
product_id: +o.product_id,
product_uom_qty: +o.product_uom_qty,
price_unit: +o.price_unit
}]));
let body = {
partner_id: 150,
user_id: 6,
workflow_id: 1,
order_line
}
let raw = JSON.stringify(body);
console.log(raw);
var requestOptions = {
method: 'POST',
headers: myHeaders,
body: raw,
redirect: 'follow'
}
fetch("https://mysite/api/sale.order/create?api_key=123", requestOptions)
.then(response => response.text())
.then(result => console.log(result))
.catch(error => console.log('error', error))
})
the file is located on the path ./ and contain the common order sales from your store.
order.json
[
{
"sku": "123",
"product_id": "6",
"price_unit": "9.7",
"product_uom_qty": "1.0000"
},
{
"sku": "456",
"product_id": "7",
"price_unit": "9.8",
"product_uom_qty": "1.0000"
}
]
Don't create JSON yourself, but use an existing well working serialization method (like the builtin JSON.stringify)
let orders = [
{
sku: 123,
product_id: 6,
price_unit: 9.7,
product_uom_qty: 1.0,
},
{
sku: 456,
product_id: 7,
price_unit: 9.8,
product_uom_qty: 1.0,
},
];
let order_line = orders.map(o => ([0,0, {
product_id: o.product_id,
product_uom_qty: o.product_uom_qty,
price_unit: o.price_unit
}]));
let body = {
partner_id: 150,
user_id: 6,
workflow_id: 1,
order_line
}
let raw = JSON.stringify(body);
console.log(raw);
Mind that I initialized orders from an array instead of reading it from the disk, but that doesn't really matter, assuming that orders.json contains valid JSON data. But I noticed, the datatypes in your orders.json differs from the datatypes in the data posted through postman. Ie for instance price_unit is a string in your JSON, but a number in your posted data. You will need to fix that. I fixed this in the orders array in my snippet.
If you can't influence the structure of your json, you can also convert between string and number while creating your object like this (notice the + in front of the property access, which converts the string from your order object into a number)
let order_line = orders.map(o => ([0,0, {
product_id: +o.product_id,
product_uom_qty: +o.product_uom_qty,
price_unit: +o.price_unit
}]));
I also used Array.map instead of the for loop ...
This will create a valid JSON string that can be correctly parsed at the server.
Related
I am fetching data from a trivia api, however the data I am retrieving is not in a format where I can readily display it, is there a problem with how I am fetching / storing the data? what is an easy fix?
Fetching & storing
React.useEffect(() => {
fetch("https://opentdb.com/api.php?amount=5&category=9&difficulty=medium")
.then(res => res.json())
.then(info => setData(info.results.map(item => {
return {
type: item.type,
question: item.question,
correct_answer: item.correct_answer,
incorrect_answers: item.incorrect_answers,
id: nanoid()
}})))
}, [])
An example of how the raw data is currently being displayed
{
"type": "multiple",
"question": "What is the German word for "spoon"?",
"correct_answer": "Löffel",
"incorrect_answers": [
"Gabel",
"Messer",
"Essstäbchen"
],
"id": "8IfTTvpoQd8DaJ1Hx941a"
},
as can see from above it is displaying the data as its raw special entities.
You can install the package 'he' by following command
npm install he
in js
import { decode } from 'he';
const json = {
type: 'multiple',
question: 'What is the German word for "spoon"?',
correct_answer: 'Löffel',
incorrect_answers: ['Gabel', 'Messer', 'EssstÄbchen'],
id: '8IfTTvpoQd8DaJ1Hx941a',
};
const jsonString = JSON.stringify(json, null, 4);
const decodedString = decode(jsonString)
in your HTML
<pre>
<code>{decodedString}</code>
</pre>
The API I am using has a nested string array it seems, I need to extract the path from it, but I cannot figure out how....
This is a break down of what I need to access.
the productimage is wrapped in quotes...
[
{title: "Item 1",
productimage: "[{"1":{"size":"75x75","path":"/10000/img.jpg"}]"
},
{title: "Item 2",
productimage: "[{"1":{"size":"75x75","path":"/20000/img.jpg"}]"
}
]
I am trying to access the image path...
The problem seems to be reading the string, I have attempted to treat it like an array, and a string and get mixed results..
Edited:
here is the entire productimages object, it is coming from an apache database that i have no control over.
productimages: "[{"1":{"size":"75x75","path":"/100000/101819-75x75-A.jpg"}},{"2":{"size":"222x222","path":"/100000/101819-600x600-A.jpg"}},{"3":{"size":"328x328","path":"/100000/101819-600x600-A.jpg"}}]"
my current axios call looks like this.
async function handleSubmit(searchData) {
if (searchData) {
const payload = searchData;
try {
const response = await axios({
url: `${baseUrl}q=*:*&fq=title:${payload}&fq=storeid:1234
method: "get",
});
//Set Hook
setData(response.data.response.docs);
} catch (error) {
console.error(error);
}
}
}
Here is the response data that is being set..
{productid: 1234, itemups: 1234, title: "productname", productimages: "[{"1":{"size":"75x75","path":"/100000/101819-75x75-A.jpg"}},{"2":{"size":"222x222","path":"/100000/101819-600x600-A.jpg"}},{"3":{"size":"328x328","path":"/100000/101819-600x600-A.jpg"}}]", productcount: 7}
I can get everything out of this, except the image.
You've to parse productimage:
const parsedArray = array.map(obj => {
let path = '';
try {
const productimage = JSON.parse(`${obj.productimage}`);
path = productimage[0].path
} catch(err) {
console.error(err)
}
return { ...obj, path }
});
[EDIT]
Axios response:
axios() // some axios call
.then(res => res.data)
.then(array => {
// ... here you can transform your array
})
Also make sure your json is properly formatted.
{
[
{"title": "Item 1",
"productimage": "[{"1":{"size":"75x75","path":"/10000/img.jpg"}]"
]
}
I have some problem to post json array with flutter.
When I hit api with postman using json it works. Screenshot postman:
Since I know on body just accept Map <String,String> CMIIW
so i turn body into like this
List<Map<String,String>> products = [
{
"product_id": "1",
"buy_quantity": "1",
"product_price": "1000",
"is_voucher": "0",
},
{
"product_id": "2",
"buy_quantity": "2",
"product_price": "2000",
"is_voucher": "0",
},
];
final String jsonProduct = json.encode(products);// here im trying to
Map<String,String> _body = {
"buyer_id": '',
"buyer_firstname": postCart.buyerFirstname,
"phone_number": postCart.phoneNumber,
"transaction_total_price": postCart.transactionTotalPrice.toString(),
"voucher_id": 0.toString(),
"voucher_code": 0.toString(),
"payment_id": postCart.paymentId.toString(),
"payment_name": postCart.paymentName,
"products" : jsonProduct
};
but i still got error,
thanks!
I'm assuming that you are using the http package.
This is an example of how to make an HTTP POST request with a json payload on the body:
Future<Lead> createLead(String clientName, String clientEmail, String clientPhone, String eventId) async {
// Create storage
final storage = new FlutterSecureStorage();
// Get API url from env
String url = (DotEnv().env['BASE_URL'] + "/leads/create");
String authToken = await storage.read(key: 'api-token');
// Create some request headers
Map<String, String> requestHeaders = {
'Content-type': 'application/json',
'Accept': 'application/json',
'X-Token': authToken
};
final response = await http.post(
url,
// enconde some JSON data on the request body
body: json.encode(
{
'event_id': eventId,
'name': clientName,
'phone': clientPhone,
'email': clientEmail
}
),
headers: requestHeaders
);
if (response.statusCode == 200) {
final leadsJson = json.decode(response.body);
Lead lead = Lead.fromJson(leadsJson);
return lead;
} else {
// If that response was not OK, throw an error.
// throw Exception('Failed to load post');
return null;
}
}
Hope it helps.
If you want to achieve the JSON in the screen shot you need to make several changes.
Notice that the list of products shows unquoted integers when converted to JSON, so these need to be kept as ints in your Dart class. (Note the preferred syntax, too.)
var products = <Map<String, int>>[
{
'product_id': 1,
'buy_quantity': 1,
'product_price': 1000,
'is_voucher': 0,
},
{
'product_id': 2,
'buy_quantity': 2,
'product_price': 2000,
'is_voucher': 0,
},
];
The type of _body needs to change because it now contains a list as well as strings. (Keep the rest the same - like the toStrings - as your screenshot shows that even the integers are quoted as strings.)
var _body = <String, dynamic>{
'buyer_id': '',
'buyer_firstname': postCart.buyerFirstname,
'phone_number': postCart.phoneNumber,
'transaction_total_price': postCart.transactionTotalPrice.toString(),
'voucher_id': 0.toString(),
'voucher_code': 0.toString(),
'payment_id': postCart.paymentId.toString(),
'payment_name': postCart.paymentName,
'products': products
};
Lastly json and (preferably) utf8 encode the resulting map, and pass that as the body parameter of your http.post.
var bytes = utf8.encode(json.encode(_body));
await http.post(url, body: bytes, headers: someHeaders);
One of your headers could be content-type: application/json;encoding=utf-8 to tell the server that you are sending UTF8 encoded JSON.
I'm trying to retrieve array of objects properties but I cannot retrieve any properties apart from _id in the object. But I can retrieve the whole array at once or even the individual objects inside array but not the properties that are within each object.
This is the JSON structure
[{
"_id": "5a82df96f2730d4bb177ffe1",
"project_id": 123,
"project_name": "proj11",
"tasks": [],
"links": [],
"__v": 0,
"upsert": true
}]
This the code
taskDelay(req,res) {
ProjectMaster.find({ "project_id": 123 })
.then(response => {
var result = response[0];
var resultId = response[0]._id;
var resultName = response[0].project_name;
var resultProjId = response[0].project_id;
console.log(result); // outputs the object
console.log(resultId); // outputs 5a82df96f2730d4bb177ffe1
console.log(resultName); // undefined
console.log(resultProjId); // undefined
res.status(200).send(response)
})
.catch(error => res
.status(400)
.send(error));
}
What might be the cause for showing undefined for other properties??
Finally, I found the solution for this issue. It is with the data format mongodb sending in response. I tried to do JSON.parse but it didn't work. toObject() did the trick.
taskDelay(req,res) {
ProjectMaster.find({ "project_id": 123 })
.then(response => {
var result = response[0];
var resultId = response[0]._id;
var resultName = response[0].toObject().project_name;
var resultProjId = response[0].toObject().project_id;
console.log(result); // outputs the object
console.log(resultId); // outputs 5a82df96f2730d4bb177ffe1
console.log(resultName); // outputs 123
console.log(resultProjId); // outputs proj11
res.status(200).send(response)
})
.catch(error => res
.status(400)
.send(error));
}
I did a mistake of not reading mongoose document completely! This behaviour is regarding with mongoose data formats.
So I am practising some nodejs and this time I am playing around with steam api and json objects. But I am having some problems.
So, from the Steam api,
http://steamcommunity.com/profiles/<steamid>/inventory/json/730/2
I got the json from this code,
var request = require('request');
var url = "http://steamcommunity.com/profiles/<steamid>/inventory/json/730/2"
request({
url: url,
json: true
}, function (error, response, body) {
if (!error && response.statusCode === 200) {
var json = JSON.parse(body);
console.log(body)
}
});
And the json looks lite this, json-link
From the json I want to take out the classid and instanceid from each of the items, but there comes the problem. I don't know how. I know I need to parse it but nothing more unfortunately.
Would be very helpful if someone could explain how, or link a guide/tutorial so I can learn.
Thanks!
EDIT:
var request = require('request');
var _ = require('lodash');
var url = "http://steamcommunity.com/profiles/76561198007691048/inventory/json/730/2";
request({
url: url,
json: true
}, function jsonParse(error, response, data) {
console.log(data.rgDescriptions);
var item = getItems(data.rgDescriptions);
console.log(item);
}
);
function getItems(data){
var item = data;
if(!item){
return "error";
}
return _(item).keys().map(function(id){
return _.pick([id], "name");}).value();
Console give me this; [ {}, {}, {}, {}, {}, {}, {}, {},
{},.... ]
JSON look like this;
'1293508920_0':
{ appid: '730',
classid: '1293508920',
instanceid: '0',
icon_url: '-9a81dlWLwJ2UUGcVs_nsVtzdOEdtWwKGZZLQHTxDZ7I56KU0Zwwo4NUX4oFJZEHLbXU5A1PIYQNqhpOSV-fRPasw8rsUFJ5KBFZv668FF4u1qubIW4Su4mzxYHbzqGtZ-KGlz8EuJcg3rnE9NiijVe3_UY-Zzr2JJjVLFEEeiQRtg',
icon_drag_url: '',
name: 'Shadow Case',
market_hash_name: 'Shadow Case',
market_name: 'Shadow Case',
name_color: 'D2D2D2',
background_color: '',
type: 'Base Grade Container',
tradable: 1,
marketable: 1,
commodity: 1,
market_tradable_restriction: '7',
},
'1644880589_236997301':
{ appid: '730',
classid: '1644880589',
instanceid: '236997301',
icon_url: '-9a81dlWLwJ2UUGcVs_nsVtzdOEdtWwKGZZLQHTxDZ7I56KU0Zwwo4NUX4oFJZEHLbXU5A1PIYQNqhpOSV-fRPasw8rsUFJ4MAlVo6n3e1Y27OPafjBN09izq42ChfbzNvXTlGkD6p0lj7_FpNjx0VDj_UBoZ272cNfBdg48MAyB-VS3xum61Me_ot2XnqkB5QYc',
icon_drag_url: '',
name: 'MLG Columbus 2016 Mirage Souvenir Package',
market_hash_name: 'MLG Columbus 2016 Mirage Souvenir Package',
market_name: 'MLG Columbus 2016 Mirage Souvenir Package',
name_color: 'D2D2D2',
background_color: '',
type: 'Base Grade Container',
tradable: 1,
marketable: 1,
commodity: 0,
market_tradable_restriction: '7',
},
To work with complex objects or collections you can use lodash library.
So, you have json with the following format:
{
"success":true,
"rgInventory": {
"5719625206": {"id":"5719625206","classid":"1651313004","instanceid":"188530139","amount":"1","pos":1},
"5719034454": {"id":"5719034454","classid":"1649582636","instanceid":"188530139","amount":"1","pos":2},
"5718628709": {"id":"5718628709","classid":"1649582636","instanceid":"188530139","amount":"1","pos":3},
...
}
}
To extract array of required items, first of all install lodash library in your project: npm i -S, after that add this code into your file:
var _ = require('lodash');
function extractItems(data) {
var rgInventory = _.get(data, 'rgInventory');
if (!rgInventory) {
return [];
}
return _(rgInventory)
.keys()
.map(function(id) {
return _.pick(rgInventory[id], ['classid', 'instanceid']);
})
.value();
}