Categorise JSON Data [closed] - javascript

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 1 year ago.
Improve this question
Hey please don't roast me. So i have a JSON data like this
data": {
"person": [
{
"id": "xx1",
"name": "John DOe",
},
],
"person": [
{
"id": "xx2",
"name": "John Snow",
},
],
}
For example, I want to store every person data whose name contains "o". How could I do that? Thank you

JSON object ,if have duplicate keys, it will replace the first one with the most bottom one .
In your sample data, there is two "person" keys. Therefore, at the end, your said data will succumb to this.
{"data":{"person":[{"id":"xx2","name":"John Snow"}]}}
Therefore, to clarify your data, IMHO, this is supposed to be like this in the first place.
"data": {
"person": [
{
"id": "xx1",
"name": "John DOe",
},
{
"id": "xx2",
"name": "John Snow",
},
]
}
Then you can treat data.person as an array and use Array.prototype function to filter out your desired details.
The filter method should be
const personWithNameO = data.person.filter((v) => v.name.includes("o") );
console.log(personWithNameO);

Related

String separated by # and | to json with javascript [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 last month.
Improve this question
I am sorry if this is a newbie question, I am recently started to work with Javascript.
I've got a string that looks like this:
myString = "#101|Maria|Smith|Teacher|26000|Football #102|Albert|Sullivan|Pianist|85000|Swimming"
My desired outcome would be a json like the following:
[
{
"id": "101"
"Name": "Maria",
"LastName": "Smith"
"Job": "Teacher",
"Salary": "26000",
"FavSport": "Football"
},
{
"id": "102"
"Name": "Albert",
"LastName": "Sullivan"
"Job": "Pianist",
"Salary": "85000",
"FavSport": "Swimming"
}
]
I have tried using myString.split('#') but it returns an array that is still separated by vertical bars and that I am not able to separate. Any ideas are highly appreciated
Split by space " " to get first your groups
Then, split by pipe "|" to get an array of values
Then .map() your array to a new Array of objects
const myString = "#101|Maria|Smith|Teacher|26000|Football #102|Albert|Sullivan|Pianist|85000|Swimming";
const groups = myString.split(" ");
const result = groups.map((groupString) => {
const [id, Name, LastName, Job, Salary, FavSport] = groupString.split("|");
return {id: id.replace("#", ""), Name, LastName, Job, Salary, FavSport};
});
console.log(result)
Then, to convert it to a JSON String use:
const json = JSON.stringify(result);
PS: Anyways, I would highly discourage you from using that input string format

Modified JSON response from nestjs while creating API [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 2 years ago.
Improve this question
I do have json like below
[
[{
"id": "4e181c1b-0a68-425e-9eb7-df36324b6cdb",
"date_actual": {
"value": "2020-10-21T13:15:00"
},
"Date": "20201021",
"name": "abc"
},
{
"id": "4e181c1b-0a68-425e-9eb7-df36324qqq",
"date_actual": {
"value": "2020-1-21T13:15:00"
},
"Date": "2020102",
"name": "xyz"
}
]
]
want to modified like below
[
{
"id": "4e181c1b-0a68-425e-9eb7-df36324b6cdb",
"date_actual": "2020-10-21T13:15:00",
"Date": "20201021",
"name": "abc"
},
{
"id": "4e181c1b-0a68-425e-9eb7-df36324qqq",
"date_actual": "2020-10-21T13:15:00",
"Date": "2020102",
"name": "xyz"
}
]
My service class code is
const rows = await bigqueryClient.query(queryData);
console.log("old json"+JSON.stringify(rows));
return rows;
how to solve this issue using nestjs any way please help me ?
This isn't specific to NodeJS or NestJS, but is regular old JavaScript. As you have an array of values you can use the Array.prototype.map method to map to a new array
return origArray.map((val) => ({ ...val, date_actual: val.date_actual.value }))

Extracting a javascript object value by key name using regex [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 2 years ago.
Improve this question
I have scraped a webpage using BeautifulSoup and requests and following piece of code is a portion from the whole HTML page.
$create(Web.Scheduler, {
"model": '{"apt":[0]}',
"timeZone": "UTC",
"_uniqueId": "94b3535a4bdaaf329cab5b7fde996f14",
"allowEdit": false,
"people":
"[{\"id\": 1, \"name\": \"John\", \"age\": \"30\"},{\"id\": 2, \"name\": \"Jane\", \"age\": \"27\"}]",
"attributes": {},
"groupBy": "name",
});
There is an object with key "people". How can I extract the value of this key using regex i.e.
[{"id": 1, "name": "John", "age": "30"},{"id": 2, "name": "Jane", "age": "27"}]
so that I can parse it as a Python dictionary. I can confirm that string "people" (with quotes) is unique on the whole page.
So far, I've seen some solutions to extract strings enclosed in quotes. But in this case, I have to use a pattern that matches "people": and maybe "attributes" key after it can also be used as a stopping point, I have to extract what's in the middle of these two. Thanks in advance!
You can use re/ast modules to extract the data. For example:
import re
from ast import literal_eval
txt = r'''
$create(Web.Scheduler, {
"model": '{"apt":[0]}',
"timeZone": "UTC",
"_uniqueId": "94b3535a4bdaaf329cab5b7fde996f14",
"allowEdit": false,
"people":
"[{\"id\": 1, \"name\": \"John\", \"age\": \"30\"},{\"id\": 2, \"name\": \"Jane\", \"age\": \"27\"}]",
"attributes": {},
"groupBy": "name",
});
'''
data = re.search(r'("people".*?),\s*\n', txt, flags=re.S).group(1)
data = literal_eval('{' + data + '}')
# print the result:
print(data['people'])
Prints:
[{"id": 1, "name": "John", "age": "30"},{"id": 2, "name": "Jane", "age": "27"}]

How to design nested 3 level json [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 3 years ago.
Improve this question
I have an object like below:
Parent: { Child1: [ {name:'grandchild1', value:'abc', checked:true}, {name:'grandchild2', value:'pqr', checked:false} ], Child2: [ {name:'grandchild3', value:'abcd', checked:false}, {name:'grandchild4', value:'pqrs', checked:true} ], parent2{...........}.... };
How can I make it nested JSON.
Just like in root: parent1, parent2...
Child: children1, ....( Corresponding to parent)
Grandchildren: based on children
Please guide me how can I make it?
To make a JSON object you just have to follow JSON syntax
In your example it will look like:
{
"parent": {
"child1": [{
"name": "grandchild1",
"value": "abc",
"checked": "true"
}, {
"name": "grandchild2",
"value": "pqr",
"checked": "false"
}]
}
}
Please note that in JSON, string values must be written with double quotes.
If you need to get JSON string from object programmatically, you can write in javascript:
var myJSON = JSON.stringify(obj);

Parsing nested array objects in json using Java script/jQuery [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
I have a JSON file like this:
{[
{"name":"avc"},
{"name":"Anna"},
{"name":"Peter"},
{"Folder":[
{"name":"John"},
{"name":"Anna"},
{"Folder":[
{"name":"gg"},
{"name":"hh"}
]
}
]
}
]
}
It can be nested to any number of levels.
I want to traverse this file for any number of levels.
How can I achieve this using Javascript/JQuery
Thanks
You could iterate with Array#forEach and if a Folder is an array, then iterate that, too.
var a = [{ "name": "avc" }, { "name": "Anna" }, { "name": "Peter" }, { "Folder": [{ "name": "John" }, { "name": "Anna" }, { "Folder": [{ "name": "gg" }, { "name": "hh" }] }] }];
a.forEach(function iter(a) {
if (Array.isArray(a.Folder)) {
a.Folder.forEach(iter);
return;
}
console.log(a.name);
});
I read your question like this:
How can I iterate through an array of object, where an object may have
children of it's own. And every child may have children and so on...
One way is to write a recursive function, which is a function that calls itself. Here's a recursive function which will log every post into console.
Note that I've changed your model into a proper object. Also, I've added name to folder, but that can be removed if not necessary.
var a = [
{name:"avc"},
{name:"Anna"},
{name:"Peter"},
{name:"Folder name1",
folder:[
{name:"John"},
{name:"Anna"},
{name: "Folder name2",
folder:[
{name:"James"},
{name:"Clark"},
{name: "Folder name3",
folder:[
{name:"Cecilia"},
{name:"Clara"}
]},
{name:"Stephen"}
]
}
]
}
];
function read(obj){
for(var i = 0; i < obj.length; i++)
{
console.log(obj[i].name); // Or do what you need here...
if(obj[i].folder)
{
// Recursive call
read(obj[i].folder);
}
}
}
read(a);

Categories

Resources