How to update large JSON objects? [closed] - javascript

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 months ago.
Improve this question
The server sends me a data as JSON to descripe a product. A product consists of properties and nested arrays of properties, up to 4 level deep. In the Frontend the user can update deep nested values. Do I need to keep track of the path an then reconstruct the whole JSON object for my updating POST call? Or is there a more elegant way to do it? Or is the design of the backend service bad?
data as JSON
{
"productId": 10,
"features": [
{
"id": 45,
"name": "Customization",
"productOptions": [
{
"id": 1,
"color": ["red", "green"],
},
{
"id": 2,
"slogans": [
{"id": 32, "name":"Thank You"},
{"id": 33, "name":"Thanks a lot"},
],
}
]
}
]
}
input to edit slogan
<input data-id="32" type="text" name="slogan" />
edit JSON to send back to the server
const update = {"id": 32, "name":"Thank You so much!"}
// update slogan array
solgansUpdateIndex = slogans.findIndex(obj => obj.id == 32)
slogans[solgansUpdateIndex] = update
...
// update whole object for the updating POST call
{...data, //updateTree}

You should not need to send the whole object back to the server. What you should do is send a set of ids that uniquely identify which field should be updated.
Example: Let's take your data where productId is 10 and featureId is 45
You can create a controller in the backend such as updateProductSlogan(int productId, int featureId, int sloganId, String/Slogan newSlogan).
And then what this endpoint should do is get the corresponding slogan from your database with a query, using these ids and update that slogan with value of newSlogan param.
Note: If only field that contains slogans is the features field you can also hardcode that and change the method to updateProductSlogan(int productId, int sloganId, String/Slogan newSlogan). in which you will fetch the features property with no need to use an id for.

Related

Insert an array of object from an array of object into mysql database php [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 months ago.
Improve this question
I'm new to JSON and I have this JSON data that I already JSON.stringify for me to pass into my back-end(php) using AJAX. At the PHP side, I want to store the following (bizName, bizAddress, salesman, term, duedate, and area) into a table named "orderDetail " and the data inside ordersArr into another table named "orders" and use the Primary Key ID from orderDetail table as a foreign key for each orders in the "orders".
I've researched many methods for me to use, what should I use in this scenario?
Thank you!
Your JSON format has a error, in the first line you have to use "order" : instead of order and make the whole data a object by adding curly bracket in start and end. Like :
{
"order" : [{
"bizName": "Sample Business",
"bizAddress": "Sample Address",
"salesman": "store",
"term": "COD",
"dueDate": "2822-18-11",
"area": "Sample Area",
"ordersArr":[{
"itemID": 2,
"name": "Sample Item",
"qty": 1,
"price":25,
"uom": "BOX"
"amount": 25
}]
}]
}
To convert JSON to a php array use $php_array = json_decode($json,TRUE);
Rest of the code below
foreach($php_array['order'] as $order){
$sql = "INSERT INTO orderDetail
VALUES (NULL,{$order['bizName']})"; // add more values as the bizName ,if this not works first save the biz Name in another variable and then pass it in the query
mysqli_query($conn,$sql); // $conn is your connection, running the query
$order_id = mysqli_insert_id(); // returns last insert id
foreach($order['ordersArr'] as $ordersArr){
$sql = "" // write the insert SQL for table orders and run it
//use $order_id as primary key id from orderDetail table
}
}

JSON jQuery auto-fill [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
first of all, I really haven't learn any javascript at all, and I am stuck here.
I want to get these link in this JSON example.
"list": [
[
"List of recipes",
[
{
"name": "Eggs",
"links": [
{
"method": "Fry",
"link": "https://example.com/category/fry"
},
]
},
]
]
I can't elaborate with my friend's example code, can anyone help me?
Your JSON isn't valid so you need to fix that first.
When it's valid parse it with JSON.parse.
When it's parsed you'll have a nested array data structure. You need to access the links array by navigating through the tree with the array indexes.
// Returns an array
data.list[0]
// Get the second element of that array
// (the first element is the text "List of recipes")
// which is also an array
data.list[0][1]
// Access the first element of that array
// (an object) and return the links array
data.list[0][1][0].links
const json = '{"list":[["List of recipes",[{"name":"Eggs","links":[{"method":"Fry","link":"https://example.com/category/fry"}]}]]]}';
const data = JSON.parse(json);
console.log(data.list[0][1][0].links);

How to generate fake data object in javascript based on the object properties dynamically? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
I have an example json object like shown below :
{
"name": "my name",
"age": 10,
"cities": [
{
"name": "Hello",
"country": "india",
"state": "something",
"email": "myemail#gmail.com",
"is_active": true
}
],
"is_present": true
}
what i want to do is to generate an object having the same json structure with same properties with with different values and i want these values to have fake data that is appropriate for the properties.
So for a property called email , the value should always be an email and for a property called image , the value should always be an image url.
I know i can use "faker.js" library for generating fake data but how can i make it dynamic so that it works well for whatever json object i use ? how do i make it such that it will automatically figure out the right type of data to be generated by faker js depending on each of the properties.
What i have tried so far :
directly match keys in the object and generate fake data based on manual condition as to which key should generate which type of data.
do a prefix or suffix key matching to detect type of fake data
But the above methods cover only the basic cases. Is there a better way to do this ?
This isn't really an answer but I don't think there is a right answer in this situation, this is more of a logical problem rather than a technical problem. The key ingredient missing in the solution is context.
You can always Object.keys(), traverse through the json, check if its a number, email or any other data type and generate data dynamically based on that. The issue is if you encounter a number field, without context, you have no idea if its a phone number, an age or the price of something or the total population count.
The next step you could possibly steer towards is having keywords that you check for against key names and that might give you some indicator as to what the key is but that's an uphill battle.
Perhaps if you could describe the use case that you are trying to cater to, someone might help suggest an alternate path.

how to use typescript to get the length of an array of objects [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I'm trying to get the length of an an array of objects from the back-end by using API call.
Here is what a "zone" objective looks like:
[
{
"id": 1,
"name": "a"
},
{
"id": 2,
"name": "b"
},
{
"id": 3,
"name": "c"
},
{
"id": 4,
"name": "d"
}
]
and the following typescript code doesn't return the length correctly:
var lengthZones= Object.keys(this.swimbandsService.getZones()).length;
What is the correct way to get the length of an an array of objects in typescript?
I'm guessing you need to wait for the Promise to resolve first, then you can access the response and count the objects inside the array.
this.swimbandsService.getZones().then((response) => {
// might need to parse the response as json, depending on your service logic
var lengthZones = response.length;
});

How to query a json file on a server [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
How to "query" a json file on server with javascript?
Filtering on server side, with ajax request.
{"employees":[
{"firstName":"John", "lastName":"Doe"},
{"firstName":"Anna", "lastName":"Smith"},
{"firstName":"Peter", "lastName":"Jones"}
]}
Something like "select * from emplyees like 'J%'. Shold avoid using db. Just use simple json file and be able to get only filteted data from server.
Need a simple, fast way to get obly needed data, using json file instead of db. Without need to install database
Thanks in advance, best regards
Massimo
it will work http://www.jsonquerytool.com/
{
"key" : "value",
"array" : [
{ "key" : 1 },
{ "key" : 2, "dictionary": {
"a": "Apple",
"b": "Butterfly",
"c": "Cat",
"d": "Dog"
} },
{ "key" : 3 }
]
}
$.array[?(#.key=2)].dictionary.b
RESULT
[
"Butterfly"
]

Categories

Resources