JSON Response Weirdly Formatted - python

I would like to preface my question that this is the first time I've interacted with an API and JSON as I'm typically more on the Database sides of things.
With that, I'm a little confused with one of the APIs I'm currently working with.
I have a vendor that has an API that allows me to pull down some information about some of the users of that service. The problem is that the response seems to not be in JSON, or if it is it isn't a version of JSON that I have seen.
The response looks like this.
{"Header":"Field1,Field2,Field3,Field4", "Rows":["Row1Value1,Row1Value2,Row1Value3,Row1Value4","Row2Value1,Row2Value2,Row2Value3,Row2Value4"]}
Which, seems wrong with everything that I've been doing with JSON so far. I'm unable to interpret this in Python as anything use-able or Powershell.
Is this a type of format? Or is this some weird thing that this vendor has generated that isn't JSON and needs to be interpreted as it's own thing?

It looks like a half-JSON implementation; the outer containers look like JSON, and you get a JSON list for the rows, but the inner contents of Header and each row in Rows looks like a string you'll need to tokenize yourself (split on commas).

I think there is a bit of confusion here. JSON means literally just JavaScript Object Notation. Anything that parses to a valid object in JS and is limited to the data types String, Bool, Int, Float, Array and Object is JSON.
So, is this JSON? Yes, beyond doubt. Is this good JSON? Not really. Unfortunately, the idea would be that you would be able to parse a JSON object into a tabular form, but here, you would have to split things yourself.
Using simple string manipulation (split()), you can easily parse the rows and restructure them to your heart's content.

Related

I want to change the view of my localhost output of dict key-values. because its look like a mess, I want it more clearner way

messed up dictionary view
change this format to this
cleaned dictionary view
I am working on a project for self-learning, In the middle of the project while I was following up, my API's output was not cleaned and it was hard to read. I wanted my output should look like the one that the video liked in the second image. I search on the internet but didn't find any clear answer.
I got the way to Display Dict in Json format. There is a Chrome Extension named "JSON Viewer" which help to convert single line of python Dict key-values into JSON web format.

Python - parse graphql, add something, then back to the query string

I'm trying to use python to parse a graphql query, alter it, and turn it back into a string query that I can pass to the graphql server.
Specifically, I'm trying to ensure that a query will always have the pageInfo paging information, so if I'm executing a query, I will always be able to automatically page through the results, even if a user might forget that stanza in their actual query.
It seems surprisingly difficult to parse a graphql query into something useful, and then be able to go from the parsed data representation back to the query string. Is there a library that google isn't able to turn up for me?
Thanks so much for any help!
The core library includes a parse function for parsing strings into AST and a print_ast function for converting the AST back to a string.

Parse JSON within Python

I just wanted to ask what can I do to solve this issue I have.
Essentially I am making a stock checker for sneakers from Adidas, I know the endpoint to obtain the stock but the JSON data given back to me whilst readable and contains what I need also contains a bunch of other information that is unnecessary to what I am trying to do.
Example of a link to an endpoint:
http://production.store.adidasgroup.demandware.net/s/adidas-GB/dw/shop/v16_9/products/(BZ0221)?client_id=c1f3632f-6d3a-43f4-9987-9de920731dcb&expand=availability,variations,prices
This is a link to the JSON containing the stock of the shoe, price and availability. However, if you try to open it you'll see that it responds a bunch of useless info such as the description of the shoe and the price which I do not need.
A github repository that I was using to try and get to grips with the requests I am trying to make is:
https://github.com/yzyio/adidas-stock-checker/blob/master/assets/index.js
I can get it to give me the JSON response I am just trying to strip what I don't need and keep what I do need which I am finding very difficult especially in python.
Many Thanks!
Since you've said you can get a JSON response from the server than the first think you need to do is tell python to load it as JSON.
import json
data = json.loads(response_from_server)
After doing this you can now access the values in your JSON object the way you would access them via a Python dict.
data["artist"]["id"]

Reverse-enginering communication protocol

What serialization format is this, and are there any libraries to parse it back to python-native data structures or at least something easier to manage?
At least it looks like it could have a 1:1 correspondent in python.
%xt%tableFameUpdate%-1%{"season":[1.329534083671E9,"160",53255],"leaderboard":[["1001:6587656216929005792","1718","Kjeld","http:/..."],["1001:6301086609221020111","802","Asti","http://..."],["1018:995158152656680513","419","QiZOra","http://..."],["1018:8494206166685317681","364","Bingay","http://..."],["1:100000380528383","160","...","http://..."]],"multipliers":{"1001:6835768553933918921":67,"1001:4106589374707547411":0,"1001:5353968490097996024":0,"1018:1168770734837476224":0,"1018:8374571792147098127":0,"1001:4225536539330822139":0,"1:100000380528383":0,"1001:4082457720357735190":68,"1001:1650191466786177826":0,"1001:4299232509980238095":38,"1001:7604050184057349633":0,"1001:6587656216929005792":0,"1001:3852516077423175846":0,"1001:888471333619738847":9,"1001:7823244004315560346":0,"1001:7665905871463311833":0,"1001:4453073160237910447":0,"1001:6338802281112620503":64,"1001:7644306056081384910":13,"1001:4956919992342871722":0,"1001:4126528826861913228":29,"1001:7325864606573096759":47,"1001:6494182198787618518":16,"1001:3678910058012926187":4,"1001:435065490460532259":39,"1001:5366593356123167358":0,"1001:6041488907938219046":8,"1001:6051083835382544277":5,"1001:9187877490300372546":0,"1001:482518425014054339":0}}%
if you strip off the first piece and the last percent sign it is json, which you could parse with any json parser. It looks like its using the percent signs as a sort of iterator, you could prolly split on those.
Apart from the few characters at the beginning, this looks like JSON.
%xt%tableFameUpdate%-1% is not JSON, but the rest is. There's a lot of JSON parsers for python, pick one and it should parse your data without a hitch.

Printing several binary data fields from Google DataStore?

I'm using Google App Engine and python for a web service. Some of the models (tables) I have in my web service have several binary data fields in them, and I'd like to present this data to a computer requesting it, all fields at the same time. Now, the problem is I don't know how to write it out in a way that the other computer knows where the first data ends and the other starts. I've been using JSON for all the things that aren't binary, but afaik JSON doesn't work for binary data. So how do you get around this?
You could of course separate the data and put it in its own model, and then reference it back to some metadata model. That would allow you to make a single page that just prints one data field of one of the items, but that is trappy both server and client implementation wise.
Another solution would be to put in some kind of separator, and just split the data on that. I suppose it would work and that's how you do it, but isn't there like a standardized way to do that? Any libraries I could use?
In short, I'd like to be able to do something like this:
binaryDataField1: data data data ...
binaryDataField2: data data data ...
etc
Several easy options:
base64 encode your data - meaning you can still use JSON.
Use Protocol Buffers.
Prefix each field with its length - either as a 4- or 8- byte integer, or as a numeric string.
One solution that would leverage your json investment would be to simply convert the binary data to something that json can support. For example, Base64 encoding might work well for you. You could treat the output of your BAse64 encoder just like you would a normal string in json. it looks like python has Base64 support built in, though i only use java on app engine so I can't guarantee that the linked library work in the sandbox or not.

Categories

Resources