xml = """
<Header_A>
<Child_A></Child_A>
<Header_B>
<Header_C>
<Child_A></Child_A>
</Header_C>
</Header_B>
</Header_A>
"""
would like to have a list enforced only under Header_C->Child_A as such:
{
"Header_A": {
"Child_A": null,
"Header_B": {
"Header_C": {
"Child_A": [null]
}
}
}
}
how to specify the exact path or key-value pair to achieve this?
the following enforces the list to all Child_A:
import json
import xmltodict
res = xmltodict.parse(xml, force_list={'Child_A'})
print(json.dumps(res, indent=2, sort_keys=True))
{
"Header_A": {
"Child_A": [
null
],
"Header_B": {
"Header_C": {
"Child_A": [
null
]
}
}
}
}
Related
in this json file I want to get original_ur but when i cant get it , can some one help me how to get it please
{
"orders": {
"order_items":[
{
"product_min":{
"colors":[
{
"media":[
{
"original_url": b
"https://royalpetiq.com/royalpet2/public//storage/10628/msg5212971698-890.jpg",
}
]
}
]
}
}
]
}
}
go on quicktype id site and create model from this json after just creat object of that claass and parse this json response and after that you can get valu like
x.orders.order_items[0].product_min.colors[0].media[0].original_url
here is your json path
x.orders.order_items[0].product_min.colors[0].media[0].original_url
try {
var json = """{
"orders": {
"order_items":[
{
"product_min":{
"colors":[
{
"media":[
{
"original_url": "https://royalpetiq.com/royalpet2/public//storage/10628/msg5212971698-890.jpg"
}
]
}
]
}
}
]
}
}""";
var data = jsonDecode(json);
var url = data["orders"]["order_items"][0]["product_min"]["colors"][0]
["media"][0]["original_url"];
print("Url: $url");
} catch (e) {
print(e);
}
I think your json should be like that.
Here the url has a b at the beginning and a comma after the url which is not the correct syntax for json
I am trying to define an initial JSON structure with a list and dictionaries. My code is as below:
import json
transactionSubmission = {}
documentDetails = []
documentDetails_dict = {}
generalDetails = {}
documentDetails_dict.update(generalDetails)
documentDetails.append(documentDetails_dict)
transactionSubmission.update({'documentDetails': [documentDetails]})
transactionSubmission_json = json.dumps(transactionSubmission)
print(transactionSubmission_json)
I am getting output as below:
{
"documentDetails": [
[
{
}
]
]
}
But i am expecting an output as below:
{
"documentDetails": [
{
"generalDetails": {}
}
]
}
Appreciate any help on this please..
This fixes what you are trying to do:
import json
transactionSubmission = {}
documentDetails = []
documentDetails_dict = {}
generalDetails = {}
documentDetails_dict['generalDetails'] = generalDetails
documentDetails.append(documentDetails_dict)
transactionSubmission['documentDetails'] = documentDetails
transactionSubmission_json = json.dumps(transactionSubmission)
print(transactionSubmission_json)
Output:
{"documentDetails": [{"generalDetails": {}}]}
But you can simplify things:
import json
transaction = {'documentDetails':[{'generalDetails':{}}]}
print(json.dumps(transaction))
print(json.dumps(transaction,indent=2)) # for readability
Output:
{"documentDetails": [{"generalDetails": {}}]}
{
"documentDetails": [
{
"generalDetails": {}
}
]
}
There is a way to map json objects in react-native? For example, let's say I receive the following object:
{
"dados": {
"ultimaAtualizacao": {
"nome": "Lala"
}
"nascimento": "01/01/2001"
}
}
I want to map it like this:
{
"data": {
"lastUpdate": {
"name": "Lala"
}
"dob": "01/01/2001"
}
}
Considering that fetch returns the first object, this would be possible doing the following code:
myService.get(url).then(response => this.mapObject(response, []));
//Considering state is initialized and I have the mapedEntity to return correct properties
mapObject(jsonObject, parentProperty) {
for (let property in jsonObject) {
if (Array.isArray(jsonObject[property])) {
this.mapArray(jsonObject[property], [...parentProperty,property]); //Would be something like mapObject function
} else if (typeof jsonObject[property] === 'object') {
this.mapObject(jsonObject[property],[...parentProperty,property]);
} else {
let prop = this.state;
for(let k of parentProperty) {
prop = prop[mapedEntity[k]];
}
prop[entidadeMapeada[property]] = jsonObject[property];
}
}
}
There is someway simplier to achieve this?
I have a json like this
{"Beauty_Personal_Care": {
"listingVersions": {
"v1": {
"get": "http://affiliate-feeds.snapdeal.com/feed/api/category/v1:586:680986904635?expiresAt=1455143400082&signature=gtvuofdhkeqxipadfzyf"
}
}
},
"Eyewear": {
"listingVersions": {
"v1": {
"get": "http://affiliate-feeds.snapdeal.com/feed/api/category/v1:473:630636448881?expiresAt=1455143400082&signature=gtvuofdhkeqxipadfzyf"
}
}
}
}
I want to get key value and objects of key value separately in node js backend.
Expected result:
name="Beauty_Personal_Care";
url="http://affiliate-feeds.snapdeal.com/feed/api/category/v1:586:680986904635?expiresAt=1455143400082&signature=gtvuofdhkeqxipadfzyf";
var json = {}; // your json
var result = [];
Object.keys(json).forEach(function (name) {
var data = {
name: name
};
data.url = json[name].listingVersions.v1.get;
result.push(data);
});
console.log(result);
I am trying to loop through a list of bands that I have in a json file (Using Swifty-Json), for some reason when I get down to looping through the names, it returns null.
Json
Small snippet of the JSON
[
{
"Band":{
"ID":"1",
"Name":"The Kooks"
}
},
{
"Band":{
"ID":"2",
"Name":"The Killers"
}
}
]
Swift Code
for (_, value) in json {
for (_,band) in value["Band"] {
for (_,bandname) in band["Name"] {
print("Band name: \(bandname)")
}
}
}
The above code returns:
Band name: null
Band name: null
Band name: null
Band name: null
When I try this:
for (_, value) in json {
for (_,brand) in value["Band"] {
print(band)
}
}
I get this result:
The Kooks
1
The Killers
2
Can anyone tell me what the issue is?
Since the value associated with the key "Name" is a simple string, you want to use:
for (_, value) in json {
for (_,band) in value["Band"] {
if let bandname = band["Name"].string {
print("Band name: \(bandname)")
} else {
print("No name specified")
}
}
}