Insert JSON object using jsonpath in javascript? [duplicate] - javascript

This question already has answers here:
How to insert an item into an array at a specific index (JavaScript)
(28 answers)
Closed 4 years ago.
Is there any way to insert a JSON object in the middle of another JSON object using jsonPath? I'm using Javascript in the Postman test script after I recieve a JSON object as a response, and I need to insert JSON data in the middle of the response. I can access the part of the response I want using "$..decision[0]". Is there a way to add data using "$..decision[1]" so that this:
...
"decision": [
{
"var1": 43,
"var2": 1,
}
],...
becomes this:
...
"decision": [
{
"var1": 43,
"var2": 1
},
{
"foo1": "true",
"foo2": "false"
}
],...
If I can't, is there another simple way to append data into the middle of a JSON object?

There is nothing like "JSON object" in javascript. You have Javascript objects/arrays, arrays of objects or string containing "Java Script Object Notation".
You can use JSON.stringify() and JSON.parse() to get from one to another.
"decision" in your example will be array after parsing JSON string by JSON.parse().
You can add object into middle of an array using its method splice().
Example:
var jsonstring = '...';
var obj = JSON.parse(jsonstring);
obj.decision.splice(1,0,{ "foo1": "true", "foo2": "false"});
jsonstring = JSON.stringify($obj);

Related

Converting formdata type string into JS object [duplicate]

This question already has answers here:
How to convert URL parameters to a JavaScript object? [duplicate]
(34 answers)
Closed 5 months ago.
I am using ajax to send FormData and receiving the output in the following format
Result=A&Status=Approved&Error=&ErrorCode=00000
I want to convert the same into a JS object (key value pair)
e.g.
{Result: "A", Status: "Approved", Error: "", ErrorCode: "00000"}
I can do that using string split and other string functions or regex but since the output format is consistent i was wondering if this is a known data type and if yes is there a way to convert it using native functions?
You can use Object.fromEntries in conjunction with the URLSearchParams constructor.
const obj = Object.fromEntries(new URLSearchParams(
'Result=A&Status=Approved&Error=&ErrorCode=00000'));
console.log(obj);
Take a look at new URLSearchParams(), it should be a way better solution than string manipulation.

How to convert string type value to array type in JavaScript? [duplicate]

This question already has answers here:
Safely turning a JSON string into an object
(28 answers)
Closed 2 years ago.
I'm using JavaScript and I want to convert string type value like this:
'[{"key": "a", "value": "aa"}, {"key": "b", "value": "bb"}]'
to array type, without '' quotes, like this:
[{"key": "a", "value": "aa"}, {"key": "b", "value": "bb"}].
I receive this type as an argument to a function where I have to treat the value as Array. But currently I can't push or pop items because the value is of string type.
You can use JSON.parse() to parse it into a javascript object
Assuming the string provided is a valid json array string(but it's not. I'd to fix a missing quote after value field). However you cannot do array operations in that until you convert it into a valid javascript array.
var jsonString='[{"key": "a", "value": "aa"}, {"key": "b", "value": "bb"}]';
var array=JSON.parse(jsonString);
array.push({key:'c',value:'cc'});
We can parse the data with JSON.parse(), and the data becomes a JavaScript object.
Make sure the text is written in JSON format, or else you will get a syntax error.
When using the JSON.parse() on a JSON derived from an array, the method will return a JavaScript array, instead of a JavaScript object.
Example:
var string = '[{"key": "a", "value": "aa"}, {"key": "b", "value": "bb"}]';
var arr = JSON.parse(string)
In this case in "arr" variable we will be getting 2 objects in array

pushing array of objects into another array of objects [duplicate]

This question already has answers here:
JavaScript: How to join / combine two arrays to concatenate into one array?
(3 answers)
Closed 4 years ago.
I have a newbie question regarding arrays and objects. I have an array of objects that I would like to push into another array of objects, but can't seem to get it to work correctly.
For instance, if I have data.list:
data.list=[{
a,
b,
c
}];
and I want to push data.list into another object array called data.overall so that it looks like this:
data.overall=[{
data.list,
z,
y,
x
}];
Sorry, for clarification, I want data.list to still exist as an array within data.overall. I've tried concat, but I keep getting an error in the console saying Server JavaScript error Cannot find function concat in object [object Object].
Thanks!
As you said, newbie I am trying to help you,
Javascript Objects should have always properties like below and you wanted to achieve this? Let me know if you have any questions.
From your question, I assume that you wanted to achieve this, Hope this helps to proceed next with Javascript
And also you can always check how JSON should be structured with below URL
https://jsoneditoronline.org/
var data = [{
"a": 1,
"b": 2,
"c": 3
}];
var dataNew = [{
"x": 4,
"y": 5,
"z": 6
}]
function getData(data) {
return data;
}
var modiFiedObject = [{
"data": getData(data[0]),
"dataNew": getData(dataNew[0])
}]
console.log(modiFiedObject);

C# Serialize to JS Object without quoting some values [duplicate]

This question already has answers here:
How to serialize a raw json field?
(2 answers)
Closed 5 years ago.
I am creating a javascript object from a c# object and one of the properties is a reference to a js function , but when serializing the object the value has quotes around it witch makes it a normal string and not a function.
this is the current output :
{ "x": "functionNameToBeCalled" }
But I need it to be like
{ "x": functionNameToBeCalled }
Is there anyway to do this with Json.Net or do I have to create the js object manually?
I tried using the JsonPropertyAttribute but can't figure out which property to set!!!
change the way of calling your method, something like this:
window.z= function(){ console.log('hi');}
var b = { a: 'z'}
window[b.a]();
so no need to change json serialization behavior.

Iterate over Javascript object [duplicate]

This question already has answers here:
How do I iterate over a JSON structure? [duplicate]
(13 answers)
Closed 8 years ago.
I have a Javascript object
var a = {
"tag1": "Stocks",
"acctType1": "individual",
"compare1": "contains",
"match_name1": "scrapedaccounttype",
"text1": "dog ",
"tag2": "Stocks",
"acctType2": "individual",
"compare2": "contains",
"match_name2": "scrapedaccounttype",
"text2": "cat"
}
I need to use this Javascript object to do some more math, but I am not sure about how I would be iterating over the Javascript object.
I can have any number of tags (tag1, tag2, tag3, tag4 ... ) or similarly other keys like (acctType1, acctType2, acctType3.... ) so I need to iterate over them individually and do some manipulation to use these variables in a different function.
While , for each what would help my cause here. Note that I could have any number of tags(tag1,tag2...) or comapare(compare1, compare2, compare3..)
I would need to process all of them individually.
What you have is NOT JSON. It is JavaScript defining an Object literal, commonly referred to as a JS object. JSON (JavaScript Object Notation) is a string that can be parsed to produce an object literal.
For your JS object (which has ample online documentation), you can iterate over the object keys by:
var a={"tag1":"Stocks","acctType1":"individual","compare1":"contains","match_name1":"scrapedaccounttype","text1":"dog ","tag2":"Stocks","acctType2":"individual","compare2":"contains","match_name2":"scrapedaccounttype","text2":"cat"}
Object.keys(a).forEach(function (k) {
console.log(a[k]);
});
Or:
for (var key in a) {
if (a.hasOwnProperty(key)) {
console.log(a[k]);
}
}

Categories