how to manipulate json objects in javascripts/jquery?
I use JSON.parse and JSON.stringify to manipulate.
json_flat = '{"page":"1","total":"2","ids":[{"id":"3085"},{"id":"3086"}]}'; // your flat json
json_object = JSON.parse(json_flat); //convert to an object
//Manipulation
json_object.page = "6"; //change values
delete json_object.total; //delete a value
json_flat = JSON.stringify(json_object); //convert back to flat
EDIT: Fixed some typos: JSFiddle
JSON Object manipulation in a format suitable for Visualization?
Why not just start from the parent first?
return original
.filter(d => d.parent_id === null)
.map(d => {
return {
name: d.name,
id: d.id,
parent_id: d.parent_id,
value: d.value,
logValue: d.logValue,
children: getChildren(d.id)
};
});
var dataset = [ { id: 1, name: "JOURNAL", tree_level: 1, display_order: null, parent_or_leaf_or_both: "Parent", parent_id: null, value: 4346 }, { id: 2, name: "OTHERS", tree_level: 1, display_order: null, parent_or_leaf_or_both: "Parent", parent_id: null, value: 28 }, { id: 3, name: "PATENT", tree_level: 1, display_order: null, parent_or_leaf_or_both: "Parent", parent_id: null, value: 18526 }, { id: 4, name: "A", tree_level: 2, display_order: null, parent_or_leaf_or_both: "Both", parent_id: 1, value: 144 }, { id: 5, name: "B", tree_level: 2, display_order: null, parent_or_leaf_or_both: "Both", parent_id: 1, value: 1966 }, { id: 6, name: "C", tree_level: 2, display_order: null, parent_or_leaf_or_both: "Both", parent_id: 1, value: 285 }, { id: 7, name: "D", tree_level: 2, display_order: null, parent_or_leaf_or_both: "Both", parent_id: 1, value: 23 }, { id: 8, name: "E", tree_level: 2, display_order: null, parent_or_leaf_or_both: "Both", parent_id: 1, value: 66 }, { id: 9, name: "F", tree_level: 2, display_order: null, parent_or_leaf_or_both: "Both", parent_id: 1, value: 1 }, { id: 13, name: "I", tree_level: 2, display_order: null, parent_or_leaf_or_both: "Both", parent_id: 1, value: 1 }, { id: 14, name: "J", tree_level: 2, display_order: null, parent_or_leaf_or_both: "Both", parent_id: 1, value: 1528 }, { id: 16, name: "L", tree_level: 2, display_order: null, parent_or_leaf_or_both: "Parent", parent_id: 1, value: 61 }, { id: 17, name: "M", tree_level: 2, display_order: null, parent_or_leaf_or_both: "Both", parent_id: 1, value: 153 }, { id: 18, name: "N", tree_level: 2, display_order: null, parent_or_leaf_or_both: "Both", parent_id: 1, value: 25 }, { id: 20, name: "P", tree_level: 2, display_order: null, parent_or_leaf_or_both: "Both", parent_id: 1, value: 139 }, { id: 33, name: "Company", tree_level: 2, display_order: null, parent_or_leaf_or_both: "Leaf", parent_id: 2, value: 1 }, { id: 40, name: "Meetings", tree_level: 2, display_order: null, parent_or_leaf_or_both: "Leaf", parent_id: 2, value: 19 }, { id: 42, name: "OTHERS", tree_level: 2, display_order: null, parent_or_leaf_or_both: "Leaf", parent_id: 2, value: 5 }, { id: 45, name: "Product Leaflet", tree_level: 2, display_order: null, parent_or_leaf_or_both: "Leaf", parent_id: 2, value: 1 }, { id: 48, name: "Society", tree_level: 2, display_order: null, parent_or_leaf_or_both: "Leaf", parent_id: 2, value: 1 }, { id: 52, name: "Websites", tree_level: 2, display_order: null, parent_or_leaf_or_both: "Leaf", parent_id: 2, value: 3 }, { id: 53, name: "EP", tree_level: 2, display_order: null, parent_or_leaf_or_both: "Leaf", parent_id: 3, value: 2004 }, { id: 55, name: "US", tree_level: 2, display_order: null, parent_or_leaf_or_both: "Leaf", parent_id: 3, value: 11355 }, { id: 57, name: "WO", tree_level: 2, display_order: null, parent_or_leaf_or_both: "Leaf", parent_id: 3, value: 5172 }, { id: 74, name: "ACS Med Chem Lett", tree_level: 3, display_order: null, parent_or_leaf_or_both: "Leaf", parent_id: 4, value: 66 }, { id: 684, name: "Annu. Rep. Med. Chem.", tree_level: 3, display_order: null, parent_or_leaf_or_both: "Leaf", parent_id: 4, value: 19 }, { id: 863, name: "Arch. Pharm. (Weinheim)", tree_level: 3, display_order: null, parent_or_leaf_or_both: "Leaf", parent_id: 4, value: 59 }, { id: 1029, name: "Biochem. Pharmacol.", tree_level: 3, display_order: null, parent_or_leaf_or_both: "Leaf", parent_id: 5, value: 2 }, { id: 1079, name: "Biol. Psychiatry.", tree_level: 3, display_order: null, parent_or_leaf_or_both: "Leaf", parent_id: 5, value: 2 }, { id: 1114, name: "Bioorg. Med. Chem.", tree_level: 3, display_order: null, parent_or_leaf_or_both: "Leaf", parent_id: 5, value: 644 }, { id: 1115, name: "Bioorg. Med. Chem. Lett.", tree_level: 3, display_order: null, parent_or_leaf_or_both: "Leaf", parent_id: 5, value: 1169 }, { id: 1206, name: "BMC Neurosci.", tree_level: 3, display_order: null, parent_or_leaf_or_both: "Leaf", parent_id: 5, value: 1 }, { id: 1251, name: "Brain Res.", tree_level: 3, display_order: null, parent_or_leaf_or_both: "Leaf", parent_id: 5, value: 2 }, { id: 1309, name: "Br. J. Pharmacol.", tree_level: 3, display_order: null, parent_or_leaf_or_both: "Leaf", parent_id: 5, value: 8 }, { id: 1361, name: "Bull. Korean Chem. Soc.", tree_level: 3, display_order: null, parent_or_leaf_or_both: "Leaf", parent_id: 5, value: 138 }, { id: 1573, name: "Cent. Nerv. Syst. Agents Med. Chem.", tree_level: 3, display_order: null, parent_or_leaf_or_both: "Leaf", parent_id: 6, value: 1 }, { id: 1599, name: "Chembiochem", tree_level: 3, display_order: null, parent_or_leaf_or_both: "Leaf", parent_id: 6, value: 2 }, { id: 1605, name: "Chem. Biol. Drug Des.", tree_level: 3, display_order: null, parent_or_leaf_or_both: "Leaf", parent_id: 6, value: 6 }, { id: 1649, name: "ChemMedChem", tree_level: 3, display_order: null, parent_or_leaf_or_both: "Leaf", parent_id: 6, value: 12 }, { id: 1785, name: "Clin. Exp. Gastroenterol.", tree_level: 3, display_order: null, parent_or_leaf_or_both: "Leaf", parent_id: 6, value: 1 }, { id: 2072, name: "Curr. Drug Targets CNS Neurol. Disord.", tree_level: 3, display_order: null, parent_or_leaf_or_both: "Leaf", parent_id: 6, value: 110 }, { id: 2101, name: "Curr. Med. Chem.", tree_level: 3, display_order: null, parent_or_leaf_or_both: "Leaf", parent_id: 6, value: 116 }, { id: 2106, name: "Curr. Med. Chem. CNS. Agents.", tree_level: 3, display_order: null, parent_or_leaf_or_both: "Leaf", parent_id: 6, value: 4 }, { id: 2119, name: "Curr. Opin. Chem. Biol.", tree_level: 3, display_order: null, parent_or_leaf_or_both: "Leaf", parent_id: 6, value: 13 }, { id: 2140, name: "Curr. Pharm. Des.", tree_level: 3, display_order: null, parent_or_leaf_or_both: "Leaf", parent_id: 6, value: 4 }, { id: 2152, name: "Curr. Top. Med. Chem.", tree_level: 3, display_order: null, parent_or_leaf_or_both: "Leaf", parent_id: 6, value: 16 }, { id: 2327, name: "Drug Data Rep.", tree_level: 3, display_order: null, parent_or_leaf_or_both: "Leaf", parent_id: 7, value: 19 }, { id: 2381, name: "Drugs Future", tree_level: 3, display_order: null, parent_or_leaf_or_both: "Leaf", parent_id: 7, value: 5 }, { id: 2634, name: "Eur. J. Clin. Invest.", tree_level: 3, display_order: null, parent_or_leaf_or_both: "Leaf", parent_id: 8, value: 1 }, { id: 2658, name: "Eur. J. Med. Chem.", tree_level: 3, display_order: null, parent_or_leaf_or_both: "Leaf", parent_id: 8, value: 57 }, { id: 2674, name: "Eur. J. Pharmacol.", tree_level: 3, display_order: null, parent_or_leaf_or_both: "Leaf", parent_id: 8, value: 5 }, { id: 2691, name: "Eur. Neuropsychopharmacol.", tree_level: 3, display_order: null, parent_or_leaf_or_both: "Leaf", parent_id: 8, value: 3 }, { id: 2950, name: "Fundam. Clin. Pharmacol.", tree_level: 3, display_order: null, parent_or_leaf_or_both: "Leaf", parent_id: 9, value: 1 }, { id: 3460, name: "Int. J. Neuropsychopharmacol.", tree_level: 3, display_order: null, parent_or_leaf_or_both: "Leaf", parent_id: 13, value: 1 }, { id: 3770, name: "J. Biol. Chem.", tree_level: 3, display_order: null, parent_or_leaf_or_both: "Leaf", parent_id: 14, value: 2 }, { id: 4012, name: "J. Enzyme Inhib. Med. Chem.", tree_level: 3, display_order: null, parent_or_leaf_or_both: "Leaf", parent_id: 14, value: 1 }, { id: 4183, name: "J. Life Sci.", tree_level: 3, display_order: null, parent_or_leaf_or_both: "Leaf", parent_id: 14, value: 1 }, { id: 4206, name: "J. Med. Chem.", tree_level: 3, display_order: null, parent_or_leaf_or_both: "Leaf", parent_id: 14, value: 1356 }, { id: 4251, name: "J. Nat. Prod.", tree_level: 3, display_order: null, parent_or_leaf_or_both: "Leaf", parent_id: 14, value: 2 }, { id: 4483, name: "J. Pharmacol. Exp. Ther.", tree_level: 3, display_order: null, parent_or_leaf_or_both: "Leaf", parent_id: 14, value: 165 }, { id: 4552, name: "Jpn. J. Pharmacol.", tree_level: 3, display_order: null, parent_or_leaf_or_both: "Leaf", parent_id: 14, value: 2 }, { id: 4594, name: "J. Psychopharmacol.", tree_level: 3, display_order: null, parent_or_leaf_or_both: "Leaf", parent_id: 14, value: 3 }, { id: 4603, name: "J. Receptor Ligand Channel Res.", tree_level: 3, display_order: null, parent_or_leaf_or_both: "Leaf", parent_id: 14, value: 1 }, { id: 4758, name: "The Journal of pharmacology and experimental therapeutics", tree_level: 3, display_order: null, parent_or_leaf_or_both: "Leaf", parent_id: 14, value: 1 }, { id: 4891, name: "Lett. Drug Des. Discov.", tree_level: 3, display_order: null, parent_or_leaf_or_both: "Leaf", parent_id: 16, value: 61 }, { id: 4986, name: "Medchemcomm.", tree_level: 3, display_order: null, parent_or_leaf_or_both: "Leaf", parent_id: 17, value: 8 }, { id: 5049, name: "Med. Res. Rev.", tree_level: 3, display_order: null, parent_or_leaf_or_both: "Leaf", parent_id: 17, value: 1 }, { id: 5147, name: "Mini Rev. Med. Chem.", tree_level: 3, display_order: null, parent_or_leaf_or_both: "Leaf", parent_id: 17, value: 64 }, { id: 5197, name: "Molecules", tree_level: 3, display_order: null, parent_or_leaf_or_both: "Leaf", parent_id: 17, value: 4 }, { id: 5221, name: "Mol. Pharmacol.", tree_level: 3, display_order: null, parent_or_leaf_or_both: "Leaf", parent_id: 17, value: 76 }, { id: 5283, name: "Nat. Cell Biol.", tree_level: 3, display_order: null, parent_or_leaf_or_both: "Leaf", parent_id: 18, value: 2 }, { id: 5285, name: "Nat. Chem. Biol.", tree_level: 3, display_order: null, parent_or_leaf_or_both: "Leaf", parent_id: 18, value: 4 }, { id: 5346, name: "Naunyn Schmiedebergs Arch. Pharmacol.", tree_level: 3, display_order: null, parent_or_leaf_or_both: "Leaf", parent_id: 18, value: 2 }, { id: 5413, name: "Neuropharmacology", tree_level: 3, display_order: null, parent_or_leaf_or_both: "Leaf", parent_id: 18, value: 7 }, { id: 5422, name: "Neuropsychopharmacology", tree_level: 3, display_order: null, parent_or_leaf_or_both: "Leaf", parent_id: 18, value: 8 }, { id: 5426, name: "Neuroreport", tree_level: 3, display_order: null, parent_or_leaf_or_both: "Leaf", parent_id: 18, value: 1 }, { id: 5537, name: "Nuclear medicine and biology", tree_level: 3, display_order: null, parent_or_leaf_or_both: "Leaf", parent_id: 18, value: 1 }, { id: 5549, name: "Nucl. Med. Biol.", tree_level: 3, display_order: null, parent_or_leaf_or_both: "Leaf", parent_id: 18, value: 1 }, { id: 5844, name: "Pharmacol. Biochem. Behav.", tree_level: 3, display_order: null, parent_or_leaf_or_both: "Leaf", parent_id: 20, value: 1 }, { id: 5846, name: "Pharmacological reports : PR", tree_level: 3, display_order: null, parent_or_leaf_or_both: "Leaf", parent_id: 20, value: 1 }, { id: 5862, name: "Pharmacol. Rev.", tree_level: 3, display_order: null, parent_or_leaf_or_both: "Leaf", parent_id: 20, value: 20 }, { id: 6136, name: "Proc. Natl. Acad. Sci. U.S.A.", tree_level: 3, display_order: null, parent_or_leaf_or_both: "Leaf", parent_id: 20, value: 116 }, { id: 6247, name: "Psychopharmacology (Berl.)", tree_level: 3, display_order: null, parent_or_leaf_or_both: "Leaf", parent_id: 20, value: 1 }];
var flare = createJSON(dataset);
function createJSON(original) { function getChildren(pid) { return original .filter(function(d) { return d.parent_id === pid; }) .map(function(d) { return { name: d.name, id: d.id, parent_id: d.parent_id, value: d.value, logValue: d.logValue, children: getChildren(d.id) }; }); }
return original .filter(d => d.parent_id === null) .map(d => { return { name: d.name, id: d.id, parent_id: d.parent_id, value: d.value, logValue: d.logValue, children: getChildren(d.id) }; });}
console.log(flare);
How to manipulate json objects and add key using javascript?
You need to use two forEach()
loop for that output:
var result = [{ "storeId": "4543", "type": "full", "overSerializedItems": [3548240, 91511753, 345555], }, { "storeId": "5462", "type": "half", "overSerializedItems": [2548240, 9566666], }];
result.forEach(function(obj){ obj.overSerializedItems.forEach(function(val, index){ obj.overSerializedItems[index] = {'store':val.toString()}; });});console.log(result);
Json object values manipulation in Spring-boot
Rather than using plain JDBC, it might be more interesting to use JPA (+ Spring Data) since this allows you to work with classes/objects rather than maps, for example:
@Entity
@Table(name = "migrations")
public class Migration {
@Id
private Long mignum;
private LocalDate productiondate;
private String installationtiers;
private String targetplatform;
private String apprelated;
private String appversion;
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "mignum")
private MigrationState state;
// Getters + Setters
}
@Entity
@Table(name = "migration_states")
public class MigrationState {
@Id
private Long mignum;
private String state;
private String projectleader;
// Getters + Setters
}
To make a query, you can create a repository and use JPQL:
public interface MigrationRepository extends JpaRepository<Migration, Long> {
List<Migration> findByProductiondate(LocalDate productiondate);
}
These classes (Migration
and MigrationState
) represent your database model. Since your question didn't contain all the database related information (only the query you wrote), this could be slightly different in your case.
Note: You don't really need JPA, most of this answer can be used without JPA as well, but you'll have to do the mapping between the
Map<String, Object>
andMigration
by yourself.
However, next to those classes, you also need classes that represent the output you prefer. For example, this would work for your second output:
public class SimpleStateSummaryDTO {
private String state;
private int count;
}
And this would work for your first output:
public class StateSummaryDTO {
private String state;
private List<MigrationDTO> value;
// Getters + Setters
}
Rather than using the Migration
class that represents your database, you should create a new class to represent the migration in your JSON, which we could call MigrationDTO
:
public class MigrationDTO {
private Long mignum;
private String projectleader;
private LocalDate productiondate;
private String installationtiers;
private String targetplatform;
private String apprelated;
private String appversion;
// Getters + Setters
}
Note: A DTO stands for Data Transfer Object, and represents the data that is used for communication between two processes, in your case your API and your consumer.
Now that you have these classes, you can use the Java 8 functional API to group the Migration
classes by their state, for example:
repository
.findByProductiondate(LocalDate.of(2018, 6, 6)).stream()
.collect(Collectors.groupingBy(migration -> migration.getState().getState()))
However, this would return a Map<String, List<Migration>>
, which isn't what you want. Now you could either write your own collector, or stream this map again so that you can map it to what you need.
For example, for the second output that would become:
repository
.findByProductiondate(LocalDate.of(2018, 6, 6)).stream()
.collect(Collectors.groupingBy(migration -> migration.getState().getState()))
.entrySet().stream()
.map(entry -> new SimpleStateSummaryDTO(entry.getKey(), entry.getValue().size()))
.collect(Collectors.toList());
What we do here is that for each Map.Entry<String, List<Migration>>
, we're creating a new SimpleStateSummaryDTO
containing the state and the size of the list.
The first output on the other hand requires an additional mapping of Migration
to MigrationDTO
:
repository
.findByProductiondate(LocalDate.of(2018, 6, 6)).stream()
.collect(Collectors.groupingBy(migration -> migration.getState().getState()))
.entrySet().stream()
.map(entry -> new StateSummaryDTO(
entry.getKey(),
entry.getValue().stream()
.map(migration -> new MigrationDTO(
migration.getMignum(),
migration.getState().getProjectleader(),
migration.getProductiondate(),
migration.getInstallationtiers(),
migration.getTargetplatform(),
migration.getApprelated(),
migration.getAppversion()))
.collect(Collectors.toList())))
.collect(Collectors.toList());
Note: You don't need the Java 8 functional API and you could write some traditional logic using loops. This just makes it a little easier.
Now that you have your desired output as objects, you're pretty much done, since Spring will automatically convert objects to JSON using Jackson, for example:
@GetMapping
public List<SimpleStateSummaryDTO> findSimple() {
return repository
.findByProductiondate(LocalDate.of(2018, 6, 6)).stream()
.collect(Collectors.groupingBy(migration -> migration.getState().getState()))
.entrySet().stream()
.map(entry -> new SimpleStateSummaryDTO(entry.getKey(), entry.getValue().size()))
.collect(Collectors.toList());
}
Powershell Json object manipulation
You can remove "field_four"
with the Remove
method from PSObject.Properties
:
$json_object.fields.PSObject.Properties.Remove("field_four")
Manipulate json data to fit in tree data table in ant design
It looks like you just want to do a transformation. In this case you might consider the below solution which uses Array#map and Array#reduce to output the format you need.
UPDATED SOLUTION
const projectIdToChildrenDict = apiData.reduce((dict, item) => {
item.projectIds.forEach(projectId => {
if (dict[projectId] === undefined) {
// instantiate empty array if the project id key is not found
dict[projectId] = [];
}
dict[projectId].push(item);
});
return dict;
}, {})
const data = Object.keys(projectIdToChildrenDict).map(key => {
return {
projectIds: key,
children: projectIdToChildrenDict[key]
}
})
It's not a full solution, but it should get you 90% there.
The first section projectIdToChildrenDict
is using Array#reduce to build an object. This object is like a dictionary (hence the name) where you can look up a projectId and see all of the elements that contained that project Id.
Then when we are initializing data
we are iterating over the previously built dictionary's keys (project ids) using Array#map and returning a new object of the expected output.
Related Topics
Ruby: Mass Initializing Instance Variables
Uninstall Ruby on Rails on MAC Os X 10.6
Native Threads in Ruby 1.9.1, Whats in It for Me
Shortening Socket Timeout Using Timeout::Timeout(N) Does Not Seem to Work for Me
Advice on How to Validate Names and Surnames Using Regex
Nanoc Changing the Base Path When Deploying Page in Github
Git Bash Chcp Windows7 Encoding Issue
Access Attributes/Methods Comments Programmatically in Ruby
Search for "Enabled" Users in Net-Ldap for Ruby
Nested Form_For Singular Resource
Requiring a Ruby Gem in Ruby Script Breaks Cron Job Execution
How to Convert PDF to Excel or CSV in Rails 4
How to Listen to Stdin Input Without Pausing My Script
State MAChine, Model Validations and Rspec
Ruby: How Does Constant-Lookup Work in Instance_Eval/Class_Eval