This is my nested list I want to convert this list into below nested dictionary
json_data =
[[[[[[[[[[[['{{app.elastic.hostname}}'], 'hostname:'],
[['{{app.elastic.scheduleDuration}}'], 'scheduleDuration:'],
[['{{app.elastic.dataDuration}}'], 'dataDuration:'],
[['{{app.elastic.displaySummary}}'], 'displaySummary:'],
[['{{app.elastic.recordSize}}'], 'recordSize:']]]],
'elastic:']]]],
'app:'],
[[[[[[[[[['{{spring.profiles.active}}'], 'active:']]]], 'profiles:'],
[[[[[['{{spring.redis.host}}'], 'host:'],
[[[[[['{{spring.redis.sentinel.master}}'], 'master:'],
[['{{spring.redis.sentinel.nodes}}'], 'nodes:']]]],
'sentinel:'],
[['{{spring.redis.password}}'], 'password:'],
[['{{spring.redis.port}}'], 'port:']]]],
'redis:']]]],
'spring:']]]
and I want to expect below on structure
list_dic = {
"app": {
"elastic": {
"hostname": "{{app.elastic.hostname}}",
"scheduleDuration": "{{app.elastic.scheduleDuration}}",
"dataDuration": "{{app.elastic.dataDuration}}",
"displaySummary": "{{app.elastic.displaySummary}}",
"recordSize": "{{app.elastic.recordSize}}"
}
},
"spring": {
"profiles": {
"active": "{{spring.profiles.active}}"
},
"redis": {
"host": "{{spring.redis.host}}",
"sentinel": {
"master": "{{spring.redis.sentinel.master}}",
"nodes": "{{spring.redis.sentinel.nodes}}"
},
"password": "{{spring.redis.password}}",
"port": "{{spring.redis.port}}"
}
}
},
The above nested dictiionary is my expected result.
The is my python recursive function to get above nested dictionary
def gen_final_json(list_obj):
list_dic = {}
for i in list_obj:
if isinstance(i, list):
list_dic.update(gen_final_json(i))
else:
list_dic[i]={}
return list_dic
final_dict = gen_final_json(json_data)
print(final_dict)
The above code produce wrong result I need above nested result.
Related
With jq, how can I transform the following:
{
"root": {
"branch1": {
"leaf": 1
},
"branch2": {
"leaf": 2
},
"branch3": {
"leaf": 3
}
},
"another-root": {
"branch": 123
},
"foo": "bar"
}
to this:
{
"root": {
"branch1": {
"leaf": "updated"
},
"branch2": {
"leaf": "updated"
},
"branch3": {
"leaf": "updated"
}
},
"another-root": {
"branch": 123
},
"foo": "bar"
}
🤦 Apparently [] can be used on object too. I had though it was only for lists.
The following was all I needed.
.root[].leaf="updated"
First you need to parse the json and then modify the resulting object as required using for ... in statement (example below)
const flatJSON = '{"root":{"branch1":{"leaf":1},"branch2":{"leaf":2},"branch3":{"leaf":3}},"another-root":{"branch":123},"foo":"bar"}';
const parsedJSON = JSON.parse(flatJSON);
const root = parsedJSON.root;
for (let property in root) {
root[property].leaf = "updated"; (or root[property]["leaf"] = "updated";)
}
If you want to use jquery you have to replace for ... in statement with jQuery.each() method that iterates over both objects and arrays.
Don't forget to convert it back to json with JSON.stringify() method (if required).
Hope that this helps.
All the best.
I'm trying to parse and mofidy JSON with Groovy. Source JSON from REST API looks like:
[
{
"id":27858,
"type":"ad",
"stats":[
{
"day":"2021-01-21",
"sex":[
{
"impressions_rate":0.349,
"value":"f"
},
{
"impressions_rate":0.621,
"value":"m",
"clicks_rate":0.22
}
],
"age":[
{
"impressions_rate":0.217,
"value":"18-21"
}
]
},
{
"day":"2021-02-25",
"sex":[
{
"impressions_rate":0.349,
"value":"f"
},
{
"impressions_rate":0.651,
"value":"m"
}
],
"age":[
{
"impressions_rate":0.217,
"value":"18-21"
}
]
}
]
},
{
"id":565291,
"type":"ad",
"stats":[
{
"day":"2021-03-21",
"sex":[
{
"impressions_rate":0.78,
"value":"f",
"clicks_rate":0.33
},
{
"impressions_rate":0.551,
"value":"m"
}
],
"age":[
{
"impressions_rate":0.17,
"value":"18-21"
}
]
}
]
}
]
It's an array with some ids and data for them. I want to grab id, day inside stats array and elements from sex array. After all manipulations my JSON should be like this:
[
{
"id": 27858,
"day": "2021-01-21",
"impression_rate": 0.349,
"value": "f"
},
{
"id": 27858,
"day": "2021-01-21",
"impression_rate": 0.621,
"value": "f",
"clicks_rate": 0.22
},
{
"id": 27858,
"day": "2021-02-25",
"impressions_rate":0.349,
"value":"f"
},
{
"id": 27858,
"day": "2021-02-25",
"impressions_rate":0.651,
"value":"m"
},
{
"id": 565291,
"day": "2021-03-21",
"impressions_rate":0.78,
"value":"f",
"clicks_rate":0.33
},
{
"id": 565291,
"day": "2021-03-21",
"impressions_rate":0.78,
"value":"f",
"clicks_rate":0.33
}
]
So, the main goal is - loop through all ids -> elements in sex array (for each id) and add to these elements day and id mapped fields. I tried to start with empty map with inject, but after 1 hour of debugging i still can't achieve desired output, so maybe better to loop through existed values in array? But I can't even reach sex array.
import groovy.json.*
def json = new JsonSlurper().parseText '''...'''
List expected = json.inject([]){ r, e ->
Map ids = e.findAll {
k, v -> k == "id"
}
e.each{ k, v ->
if( (v.any{ it.sex } )
v.each{ r << ids + it }
}
return r
}
If you have nested structures, that contain again nested structures,
a good option, to get a flat result, is to use collectMany; like
collect it transforms each item of the iterated container, but the
results gets concated.
E.g. you can collectMany on your outer data, then again on the
stats, and finally just collect over sex.
def data = new groovy.json.JsonSlurper().parse("data.json" as File)
println data.collectMany{ o ->
o.stats.collectMany{ i ->
i.sex.collect{ it + [id: o.id, day: i.day] }
}
}
// [[impressions_rate:0.349, value:f, id:27858, day:2021-01-21],
// [impressions_rate:0.621, value:m, clicks_rate:0.22, id:27858, day:2021-01-21],
// [impressions_rate:0.349, value:f, id:27858, day:2021-02-25],
// [impressions_rate:0.651, value:m, id:27858, day:2021-02-25],
// [impressions_rate:0.78, value:f, clicks_rate:0.33, id:565291, day:2021-03-21],
// [impressions_rate:0.551, value:m, id:565291, day:2021-03-21]]
I'm trying to create a test API in mockable.
What am I trying to create?
I'm trying to build an Json object with a Nested object which holds another nested object.
Example for use: store object => Store info => product list
What I expect to create
{
"Object": {
"id": 0,
"name": "Nova",
"nestedObject": {
{
"id": 1,
"name": "NestedNestedObject1",
},
{
"id": 2,
"name": "NestedNestedObject2",
},
}
Result I'm getting:
Error: Parse error on line 11:
...: { {
----------------------^
Expecting 'STRING', '}'
At NestedNestedObject2
How do I create a nested, nested object? If I'm correct mockable accepts pure Json
It depends on what you want to create and that depends on your API. The actual problem is that your JSON is not valid.
After your nestedObject there is just a { and that is wrong. In this case I assume you want to have an array of nestedObject (and perhaps also name should be nestedObjects) so fix would be (see the array []):
{
"Object": {
"id": 0,
"name": "Nova",
"nestedObject": [
{
"id": 1,
"name": "NestedNestedObject1"
},
{
"id": 2,
"name": "NestedNestedObject2"
}
]
}
}
My json data is as follows:
"query": {
"count": 1,
"url": [
{
"execution-start-time": "1"
},
{
"execution-time": "745"
}
],
"service-time": "1481"
},
results": {
"div": {
"class": "makers",
"ul": {
"li": [
{
"a": {
"href": "nokia_x2_dual_sim-6383.php",
"img": {
"src": "Nokia-X2-Dual-SIM.jpg"
},
"strong": {
"br": null,
"content": "Nokia\nX2 Dual SIM"
}
}
},
{
"a": {
"href": "nokia_xl-6148.php",
"img": {
"src": "nokia-xl.jpg",
},
"strong": {
"br": null,
"content": "Nokia\nXL"
}
}
}
]
Now I want to extract the "content" part from this json data using jsonpath.I am using jsonpath-0.8.0.js for this purpose.
I have tried to parse the json data in the following way:
function ParseData(data) {
var result = jsonPath(data, "$.query.results[*].ul.li[*].strong.content");
$('#carousel').empty();
var html = "";
for (i = 0; i < result.length; i++) {
// do something
}
and I was expecting to get Nokia\nX2 Dual SIM and Nokia\nXL as output but this code of mine does not return anything.I cannot understand the problem.
How do I extract the content data from this json? please help.
Friends, I finally got the answer to my problem.I used the following code to extract content from the above given json data.
var result = jsonPath(data, "$.query.results[*].ul.li[*].a.strong.content");
Now I am getting the output data as expected.
I am having a JSON data like below.
{
"divisions": [{
"name": "division1",
"id": "div1",
"subdivisions": [{
"name": "Sub1Div1",
"id": "div1sub1",
"schemes": [{
"name": "Scheme1",
"id": "scheme1"
}, {
"name": "Scheme2",
"id": "scheme2"
}]
}, {
"name": "Sub2Div1",
"id": "div1sub2",
"schemes": [{
"name": "Scheme3",
"id": "scheme3"
}]
}
]
}]
}
I want to read this into a TreeStore, but cannot change the subfields ( divisions, subdivisions, schemes ) to be the same (eg, children).
How can achieve I this?
When nested JSON is loaded into a TreeStore, essentially the children nodes are loaded through a recursive calls between TreeStore.fillNode() method and NodeInterface.appendChild().
The actual retrieval of each node's children field is done within TreeStore.onNodeAdded() on this line:
dataRoot = reader.getRoot(data);
The getRoot() of the reader is dynamically created in the reader's buildExtractors() method, which is what you'll need to override in order to deal with varying children fields within nested JSON. Here is how it's done:
Ext.define('MyVariJsonReader', {
extend: 'Ext.data.reader.Json',
alias : 'reader.varijson',
buildExtractors : function()
{
var me = this;
me.callParent(arguments);
me.getRoot = function ( aObj ) {
// Special cases
switch( aObj.name )
{
case 'Bill': return aObj[ 'children' ];
case 'Norman': return aObj[ 'sons' ];
}
// Default root is `people`
return aObj[ 'people' ];
};
}
});
This will be able to interpret such JSON:
{
"people":[
{
"name":"Bill",
"expanded":true,
"children":[
{
"name":"Kate",
"leaf":true
},
{
"name":"John",
"leaf":true
}
]
},
{
"name":"Norman",
"expanded":true,
"sons":[
{
"name":"Mike",
"leaf":true
},
{
"name":"Harry",
"leaf":true
}
]
}
]
}
See this JsFiddle for fully working code.