Python 3.6 - Parse json response with dict inside dict - json

I'm being driven insane because I can't parse this json response. I've tried many different things and nothing works properly... Could you help me?
The this is the file I am parsing:
{
"info": {
"funds": {
"asset": {
"net": "12516.000",
"total": "0"
},
"borrow": {
"btc": "0",
"cny": "0",
"ltc": "0"
},
"free": {
"btc": "0",
"cny": "0",
"ltc": "0",
"eth": "0"
},
"freezed": {
"btc": "0",
"cny": "0",
"ltc": "0",
"eth": "0"
},
"union_fund": {
"btc": "0",
"ltc": "0"
}
}
},
"result": true
}
I just want something like:
#What I want to get the "net" which is "12516.000", so I tried this:
funds = response['info']['funds']['asset']['net']
funds = response[0] returns { as answer, and funds = response[1] gives me r as a response, and finally if I try funds = response['info'] I get this type error: TypeError: string indices must be integers

You haven't actually parsed the JSON, it's just being read as a string, so response[0] returns the first character of the JSON string, or {. To parse the JSON string,
import json
json.loads(response)['info']['funds']['asset']['net']
which is the pattern you're expecting. More details about the json library can be found here.

I appreciate the answer of #kevmo314.
Sometimes your response may contain leading and trailing whitespaces. You can remove it using strip().
Note: when you get response, basically you get as a string (in some cases, it may be different) which can represent Python objects like list, dictionary etc.
So it's necessary to convert them back into their original form before performing any operations on them.
Below is the working code.
import json
response = response.strip()
response = json.loads(response)
funds = response["info"]["funds"]["asset"]["net"]
print(funds)

Related

Parsing string containing complex struct

I'm receiving a third-party API payload response like:
{
"message": "Validation failed because [{reason=CONDITIONAL_INVALID_VALUE, field=/targetingCriteria, batchIndex=0, type=INVALID_VALUE, message=/locale cannot be set to en if urn:li:adTargetingFacet:interfaceLocales is set to urn:li:locale:it_IT, parameters={field1=/locale, value2=urn:li:locale:it_IT, value1=en, field2=urn:li:adTargetingFacet:interfaceLocales, key=}}, {reason=FIELD_VALUE_TOO_LOW, field=dailyBudget, batchIndex=0, type=INVALID_VALUE, message=/dailyBudget/amount value 1 cannot be lower than 10.00, parameters={min=10.00, field=/dailyBudget/amount, costType=CPM, type=SPONSORED_UPDATES, value=1, key=}}]",
"status": 400
}
and I'd like to transform in something like:
{
"errors": [{
"reason": "CONDITIONAL_INVALID_VALUE",
"field": "/targetingCriteria",
"batchIndex": "0",
"type": "INVALID_VALUE",
"message": "/locale cannot be set to en if urn:li:adTargetingFacet:interfaceLocales is set to urn:li:locale:it_IT",
"parameters": "{field1=/locale, value2=urn:li:locale:it_IT, value1=en, field2=urn:li:adTargetingFacet:interfaceLocales, key=}"
},
{
"reason": "FIELD_VALUE_TOO_LOW",
"field": "dailyBudget",
"batchIndex": "0",
"type": "INVALID_VALUE",
"message": "/dailyBudget/amount value 1 cannot be lower than 10.00",
"parameters": "{min=10.00, field=/dailyBudget/amount, costType=CPM, type=SPONSORED_UPDATES, value=1, key=}"
}
]
}
But I'm struggling to find a clear golang approach to this problem, the main problems are:
no valid json is available: word are not correctly quoted with "
= symbol instead of :
nested graphs bracket
I'm currently try to transform in a valid json string and then parse as json but I have various problem with nested elements
Any idea?
EDIT: This is what I've done right now: https://play.golang.org/p/B7bdPCJoHc2

Parsing json in json Groovy Katalon Studio

I got a JSON text which I should parse, but for some reason I can't parse it because it has another array inside. My JSON looks like that:
{
"statementId": "1",
"movements": [
{
"id": 65,
"date": "2019-02-05",
"number": 32,
"balance": -4.62,
"purpose": "1"
},
{
"id": 1,
"date": "2019-02-05",
"number": 22,
"balance": -3,
"purpose": "23"
},
{
"id": 32,
"date": "2019-02-05",
"number": 12,
"balance": -11,
"purpose": "2"
}
],
"startPointer": "1122",
"endPointer": "3333"
}
I am using JsonSlurper. I want to know if it is possible to catch all the data inside "movements", I have tried to use this script:
JsonSlurper slurper = new JsonSlurper()
Map parsedJson = slurper.parseText(bodyContent)
String parsed_movements = parsedJson["movements"]
I have no problem with parsing single strings, like statementId or startPointer, but when I try to parse movements with my script it gives me result as null. I have also tried parsedJson["movements"][0] to catch first movement but it also gives me an error.
I have found a lot of things about json parsers on internet and also on stackoverflow but nothing what I seek. I really don't think that it is a duplicate question.
EDIT: I tried for statement also to put each object in array like that:
def movements_array = []
for(def i = 0; i < parsedJson.movements.size(); i++) {
movements_array << parsedJson.movements[i].id
println(movements_array)
}
But it gives me an error: Cannot invoke method size() on null object, because parsedJson.movements is null.
When you do:
String parsed_movements = parsedJson["movements"]
You're sticking a map into a String, which isn't what you want.
Given the json in your question, you can just do
def movementIds = new JsonSlurper().parseText(bodyContents).movements.id
To get a list of [65, 1, 32]
If you're getting NPEs I assume the json isn't what you show in the question

SAPUI5 access getProperty() of Json Model (undefined). The Model itself is accessible

I can't access my JSON model which has been defined in the manifest.json.
My JSON data "zCatsTestJ" looks like this:
{
"d": {
"results": [{
"status": "30",
"skostl": "6210",
"catshours": "2.50",
"ktext": "-",
"counter": "000003484040",
"mandt": "101",
"pernr": "00015822",
"usrid": "-",
"workdate": "\/Date(1477267200000)\/",
"raufnr": "6000025",
}, {
"status": "30",
"skostl": "6210",
"catshours": "2.50",
"ktext": "-",
"counter": "000003484040",
"mandt": "101",
"pernr": "00015822",
"usrid": "-",
"workdate": "\/Date(1477267200000)\/",
"raufnr": "6000025",
}]
}
}
The model seems to be accesible as sJsonDate1 is showing me the data in the console but I can't access a single date. In the end I want to loop over those dates and change the formatting.
var sJsonDate1 = this.getOwnerComponent().getModel("zCatsTestJ");
var sJsonDate2 =this.getOwnerComponent().getModel("zCatsTestJ").getProperty("/d/results/1/workdate");
console.log(sJsonDate1);
console.log(sJsonDate2);
Here is the console output where I can see the complete data.
Console sJsonDate1
But when I try to access one datapoint it says undefined
Console sJsonDate2
I have also instatiated the Model directly in the component and it is working fine. When I compare the model object from getOwnerComponent() and the new one they are nearly the same except for the local one having no aBindings
Model comparison in console
Any help will be highly apreciated. Thanks
Your JSON is invalid, status is missing " and you never close the array. I tried it locally after correcting your JSON and it worked fine on my end.
var json = {
"d": {
"results": [
{
"status": "30",
"skostl": "6210",
"catshours": "2.50",
"ktext": "-",
"counter": "000003484040",
"mandt": "101",
"pernr": "00015822",
"usrid": "-",
"workdate": "\/Date(1477267200000)\/",
"raufnr": "6000025",
}, {
"status": "30",
"skostl": "6210",
"catshours": "2.50",
"ktext": "-",
"counter": "000003484040",
"mandt": "101",
"pernr": "00015822",
"usrid": "-",
"workdate": "\/Date(1477267200000)\/",
"raufnr": "6000025",
}]
}
};
var myModel = new JSONModel(json);
this.getView().setModel(myModel, "test");
this.getView().getModel("test").getProperty("/d/results/1/workdate"); "/Date(1477267200000)/"
Here is the solution. Somehow the JsonModel was too big (667 Data Points) and wasn't loaded on the time of calling it. That's why it has worked with only two entries.
myModel.attachRequestCompleted(function() {
var sJsonDate = myModel.getProperty("/d/results/1/workdate");
console.log(sJsonDate);
});

Removing excess comma on JSON Object

Currently been working on eliminating the excess "," comma on the json object I have below.
{"rules": {
"1000": {
"action": "2",
"category": "skype",
"entity": "Private",
"id": "1000",
},
"1200": {
"action": "2",
"category": "http",
"entity": "Public",
"id": "1200",
},
"100": {
"action": "2",
"category": "ftp",
"entity": "Public",
"id": "100",
},
"0": {
"entity": "Private",
"category": "alcohol, tobacco",
"action": "1",
"id": "low",
},
"3000": {
} }}
Maybe you have some insights on what's the cleanest way to eliminate it using AngularJS.
The data was parsed from this code snippet.
var request = {
url: 'sample/uri',
method: "GET",
transformResponse: specialTransform
};
var response = $q.defer( );
$http( request ).success( function( THIS DATA -> data, status ) {
eval
var fixTrailingCommas = function (jsonString) {
var jsonObj;
eval('jsonObj = ' + jsonString);
return JSON.stringify(jsonObj);
};
fixTrailingCommas('{"rules": { "1000": { "action": "2", "category": "skype", "entity": "Private", "id": "1000" , } } }');
Please use eval here only if you completely trust incoming json, and also be aware of other eval evils as described on MDN and its note on JSON parsing
Note that since JSON syntax is limited compared to JavaScript syntax, many valid JavaScript literals will not parse as JSON. For example, trailing commas are not allowed in JSON, and property names (keys) in object literals must be enclosed in quotes. Be sure to use a JSON serializer to generate strings that will be later parsed as JSON.
You may also choose to rely on implementation of JSON2 by Douglas Crockford which uses eval internally
On current browsers, this file does nothing,
preferring the built-in JSON object. There is no reason to use this file unless
fate compels you to support IE8, which is something that no one should ever
have to do again.
But because we really need to use this library, we have to make few code modifications, e.g. simply comment out JSON type check, which will then override native browser object (or we may also introduce new JSON2 global variable)
//if (typeof JSON !== 'object') {
JSON = {};
//}
P.S. Other parsing fuctions json_parse.js and json_parse_state.js, which don't use eval, throw a syntax error
Angular part
var config = {
transformResponse: function (data, headers) {
if(headers("content-type") === "application/json" && angular.isString(data)) {
try {
data = JSON.parse(data);
} catch (e) {
// if parsing error, try another parser
// or just fix commas, if you know for sure that the problem is in commas
data = JSON2.parse(data);
}
return data;
} else {
return data;
}
}
};
$http.get("rules.json", config).success(function (data) {
$scope.rules = data;
});
So as you said, the JSON is wrongly generated on the server you are taking it from, can you change the way it is generated there? (Follow this: Can you use a trailing comma in a JSON object?)
In case you are unable to do so, you need to use something like mentioned here:
Can json.loads ignore trailing commas?
library to repair a JSON object, like: https://www.npmjs.com/package/jsonrepair
(try some online fix tool here: http://www.javascriptformat.com/)
or some regexp magic

Access object returned from Newtonsoft json DeserializeObject

Should be a no brainer, but I'm can't seem to access the elements returned from Newtonsoft's json deserializer.
Example json:
{
"ns0:Test": {
"xmlns:ns0": "http:/someurl",
"RecordCount": "6",
"Record": [{
"aaa": "1",
"bbb": "2",
},
{
"aaa": "1",
"bbb": "2",
}]
}
}
var result = Newtonsoft.Json.JsonConvert.DeserializeObject<dynamic>(somestring);
Stripping out the json up to the Record text, i can access the data without issue.
i.e. result.Recordcount
If i leave the json as shown above, can someone enlighten me how to access Recordcount?
All inputs appreciated. Thanks!
For those JSON properties that have punctuation characters or spaces (such that they cannot be made into valid C# property names), you can use square bracket syntax to access them.
Try this:
int count = result["ns0:Test"].RecordCount;