Nodejs how to use map of object? - json

Hi i have a json like this called body:
{
data: {
id:1,
name:hallo
},
paging: {
cursors: {
next: "abc"
}
}
}
so I have to save it into a map and then I'll need to print or to modify it.
If I save it with this code
var Map = require("collections/map");
var new_body = JSON.parse(body)
var map = new Map(new_body);
I have some difficulties to print for example map.paging.cursor.next, how can I do it? maybe I had to define a structure of that map before inserting the json inside it, I don't know...

Your json have errors. Correct them like this.
{
"data": {
"id": 1,
"name": "hallo"
},
"paging": {
"cursors": {
"next": "abc"
}
}
}
Then you can parse the json like this and get the paging.cursors.next value.
var map = JSON.parse(body);//This will parse the json string
Now map.paging.cursors.next will have the value "abc"

Related

How to flatten json array using typescript and Convert it as object

I am trying to convert a json array as a flat json using typescript.
I have a json as below:
"Fields" : [
{
"FieldName" : "xyz";
"FieldValue" : {
"Contents": {
" Company": "ABC"
}
}
}
]
I have to convert as below:
"xyz" : {
"Contents": {
" Company": "ABC"
}
}
Here Fields should be replaced with "xyz", FieldName and FieldValue should need to be removed.
Help me to achieve this using typescript.
If you really only want to work with that first item in your array just rearrange the data with something the following.
const json = JSON.stringify({
Fields: [
{
FieldName: "xyz",
FieldValue: {
Contents: {
" Company": "ABC"
}
}
}
]
});
const parsedJson = JSON.parse(json);
const expectedOutput = {
[parsedJson.Fields[0].FieldName]: parsedJson.Fields[0].FieldValue
};
console.log(expectedOutput);

How to retrieve array inside an array into a table using vue js

i want to retrieve some array inside an array but i dont know how, please read my explanation first
here is my json response
{
"Value": [
{
"id": "5ff97d740e788778dd66ee25",
"id_order": "ORD-434183",
"nama_pelanggan": "Herman",
"total": 80000.0,
"jumlah_item": 4.0,
"cart": [
{
"id_cart": "CART-112010",
"nama_produk": "Ayam Bakar",
"jumlah": 4.0,
"harga": 20000.0,
"subtotal": 80000.0,
"notes": "ga ada"
}
],
"admin_no": "ADM-431525",
"admin_name": "Admin",
"created_at": "2021-01-09 16:55:00",
"updated_at": "2021-01-09 16:55:00"
}
],
"Status": true,
"Messages": [
{
"Type": 200,
"Title": "TotalRows",
"Message": "1"
}
]}
as you can see the value of "value" is an array and in that array also have an array "cart"
i want to retrieve this array "cart" and put it in a table
here is how i retrieve the response using vue js Mapping an Array to Elements with v-for
var vm = new Vue({
el: '#app',
data: {
list: {
items: [],
},
}
var formData = new FormData();
formData.append('filterparams', JSON.stringify(filters));
formData.append('display', JSON.stringify(_self.list.config.display));
formData.append('auth', sessionStorage.getItem("token"));
_self.$http.post(hostapi + 'order/GetByParamsHistory', formData, {
headers: {
'Content-Type': 'multipart/form-data'
}
});
var obj = response.data;
_self.list.items = obj.Value; // the value from array "Value"
here is the table html code
and now I don't know how to insert an existing array in the array into the html table
You can access the array on the basis of the array index.
Suppose, if list contains the json response's value field then you can access it like.
list['0'].cart.

How to store json code in database table column in laravel

Hi guys i am trying to store my json data into my datatbase table column.
Here is my sample json code..this data only i am trying to store in db.
{
"template": {
"question_section": {
"section_data": [
{
"index": 1,
"section": "text section",
"properties": {
"font_size": "14",
"font_weight": "400",
"font_color": "#000000",
"row": 1
}
}
]
},
"solution_section": {
"section_data": []
}
}
}
here is my sample code:
function updateJson(field_id, from_where, section_counter, property_name, ques_section_counter, sol_section_counter)
{
if(from_where == 'question')
{
let prop_value = $('#'+field_id).val();
template_json.template.question_section.section_data[ques_section_counter-1].properties[property_name] = prop_value;
}
else if(from_where == 'solution')
{
let prop_value = $('#'+field_id).val();
template_json.template.solution_section.section_data[sol_section_counter-1].properties[property_name] = prop_value;
}
$('#code_json').html(JSON.stringify(template_json, undefined, 2));
}
This is one sample function that this is how i am getting json code and all..that above json wwhole code i would like to store.
Here is my form code:
$template->template_json = $request->input('');//this is the column
Can anyone help me on how can i store.
you have to save it as text in database like this:
$template->template_json = json_encode($request->input(''));
and then when you want to use it convert it to object before like this:
$template->template_json = json_decode($template->template_json);

Struggling to parse response from API service

Struggling to parse what appears to be a simple response from an API service (GraphQL / JSON). The response is retrieved as String based on HTTP request, and have tried different approaches (using both JsonObject/JsonParse and JsonArrayfrom com.google.gsonlibrary to get desired string elements to String, although without being able to extract anything.
Anyone willing to share suggestions how to parse and get "total" and "startsAt" from the response shown below?
{
"data": {
"viewer": {
"homes": [
{
"currentSubscription": {
"priceInfo": {
"current": {
"total": 0.5626,
"energy": 0.4336,
"tax": 0.129,
"startsAt": "2019-11-14T20:00:00+01:00"
}
}
}
}
]
}
}
}
You can get the json value like this
Assign the response to some variable like res.
For ex :
var res = {
"data": {
"viewer": {
"homes": [
{
"currentSubscription": {
"priceInfo": {
"current": {
"total": 0.5626,
"energy": 0.4336,
"tax": 0.129,
"startsAt": "2019-11-14T20:00:00+01:00"
}
}
}
}
]
}
}
var total=res.data.viewer.homes[0].currentSubscription.priceInfo.current.total;
var startAt = res.data.viewer.homes[0].currentSubscription.priceInfo.current.startsAt;
console.log('total is: ',total);
console.log('startsAt: ', startsAt)

How to get array number with groovy script in SoapUI?

I want to assert the value of a property in Json response with the use of Groovy script in SoapUI. I know a value for name but I need to know on which position the id is.
json response example:
{
"names":[
{
"id":1,
"name":"Ted"
},
{
"id":2,
"name":"Ray"
},
{
"id":3,
"name":"Kev"
}
]
}
Let's say I know that there is a name Ray, I want the position and the id (names[1].id)
Here is the script to find the same:
import groovy.json.*
//Using the fixed json to explain how you can retrive the data
//Of couse, you can also use dynamic value that you get
def response = '''{"names": [ { "id": 1, "name": "Ted", }, { "id": 2, "name": "Ray", }, { "id": 3, "name": "Kev", } ]}'''
//Parse the json string and get the names
def names = new JsonSlurper().parseText(response).names
//retrive the id value when name is Ray
def rayId = names.find{it.name == 'Ray'}.id
log.info "Id of Ray is : ${rayId}"
//Another way to get both position and id
names.eachWithIndex { element, index ->
if (element.name == 'Ray') {
log.info "Position : $index, And Id is : ${element.id}"
}
}
You can see here the output