How to prettify the json text in the Excel cell? - json

In the excel file, there are a lot of json texts like below
{"component":"{{$labels.component}}","container":"{{$labels.container}}","daemonset":"{{$labels.daemonset}}","directory":"{{$labels.directory}}","figure":"{{$value}}","instance":"{{$labels.instance}}","job":"{{$labels.job}}","name":"{{$labels.name}}","namespace":"{{$labels.namespace}}","pod":"{{$labels.pod}}","reason":"{{$labels.reason}}"}
I want to prettify the json text like this
{
"component": "{{$labels.component}}",
"container": "{{$labels.container}}",
"daemonset": "{{$labels.daemonset}}",
"directory": "{{$labels.directory}}",
"figure": "{{$value}}",
"instance": "{{$labels.instance}}",
"job": "{{$labels.job}}",
"name": "{{$labels.name}}",
"namespace": "{{$labels.namespace}}",
"pod": "{{$labels.pod}}",
"reason": "{{$labels.reason}}"
}
Is there any way to do this for any cells in my excel file?
Thanks!

If you have Excel 365 current channel then you can use this formula - where your json is in cell A1
=LET(json,A1,
r,SUBSTITUTE(json,",","," & CHAR(10)),
"{" & CHAR(10) & MID(r,3,LEN( r)-3) & CHAR(10) & "}")
First it replaces the comma by a comma plus a linebreak char(10). Then it handles the first and last linebreak.

Related

How to read Json in Google Data Studio, Big Query, Json

I am using Big Query for as a cloud data warehouse and DataStudio for vizualisation.
In Big Query I have a table with a column named data written in JSON. I only want to extract what is inside the field "city".
This formula below that someone gave me worked to extract what is inside the field "title". I used it to create a field in DataStudio.
REPLACE(REGEXP_EXTRACT(data, '"title":(.+)","ur'), "\"", "")
So, I tried in multiple ways to reuse this formula for the "city" field, but it hasn't worked. I don't understand this code.
What's inside my column data:
{
"address":{
"city":"This is what i want",
"country":"blablabla",
"lineAdresse":"blablabla",
"region":"blablabla",
"zipCode":"blablabla"
},
"contract":"blablabla",
"dataType":"blablabla",
"description":"blablabla",
"endDate":{
"_seconds":1625841747,
"_nanoseconds":690000000
},
"entreprise":{
"denomination":"blabla",
"description":"1",
"logo":"blablabla",
"blabla":"blablabla",
"verified":"false"
},
"id":"16256R8TOUHJG",
"idEntreprise":"blablabla",
"jobType":"blablabla",
"listInfosRh":null,
"listeCandidats":[
],
"field":0,
"field":0,
"field":14,
"field":"1625834547690",
"field":true,
"field":"",
"field":"ref1625834547690",
"skills":[
"field",
"field",
"field"
],
"startDate":{
"_seconds":1625841747,
"_nanoseconds":690000000
},
"status":true,
"title":"this I can extract",
"urlRedirection":"blablabla",
"validated":true
}
If anyone knows the formula to put in Data Studio to extract what's inside city and can explain it to me, this would help a lot.
Here's the formula I tried but where I got "null" result:
REPLACE(REGEXP_EXTRACT(data,'"city":/{([^}]*)}/'),"\"","") >>null
I tried this one but it wouldn't stop at the city. I got the address, the region, zipcode and all the rest after:
REPLACE(REGEXP_EXTRACT(data, '"city":(.+)","ur'), "\"", "")
It is possible to parse a JSON in a text field by ignoring any hierarchy and only looking for a specific field. In your case the field names were title and city . Please be aware that this approach is not save for user entered data: By setting the value of the "city":"\" hide \"" the script cannot extract the city.
select *,
REGEXP_EXTRACT(data, r'"title":\s*"([^"]+)') as title,
REGEXP_EXTRACT(data, r'"city":\s*"([^"]+)') as city
from(
Select ' { "address":{ "city":"This is what i want", "country":"blablabla", "lineAdresse":"blablabla", "region":"blablabla", "zipCode":"blablabla" }, "contract":"blablabla", "dataType":"blablabla", "description":"blablabla", "endDate":{ "_seconds":1625841747, "_nanoseconds":690000000 }, "entreprise":{ "denomination":"blabla", "description":"1", "logo":"blablabla", "blabla":"blablabla", "verified":"false" }, "id":"16256R8TOUHJG", "idEntreprise":"blablabla", "jobType":"blablabla", "listInfosRh":null, "listeCandidats":[ ], "field":0, "field":0, "field":14, "field":"1625834547690", "field":true, "field":"", "field":"ref1625834547690", "skills":[ "field", "field", "field" ], "startDate":{ "_seconds":1625841747, "_nanoseconds":690000000 }, "status":true, "title":"this I can extract", "urlRedirection":"blablabla", "validated":true }' as data
)

Regex: Remove Commas within quotes

I'm using NiFi and I have a series of JSONs that look like this:
{
"url": "RETURNED URL",
"repository_url": "RETURNED URL",
"labels_url": "RETURNED URL",
"comments_url": "RETURNED URL",
"events_url": "RETURNED URL",
"html_url": "RETURNED URL",
"id": "RETURNED_ID",
"node_id": "RETURNED id",
"number": 10,
...
"author_association": "xxxx",
"active_lock_reason": null,
"body": "text text text, text text, text text text, text, text text",
"performed_via_github_app": null
}
My focus is on the "body" attribute. Because I'm merging them into one giant JSON to convert into a csv, I need the commas within the "body" text to go away (to help with possible NLP later down the road as well). I know I can just use the replace text, but capturing the commas themselves is the part I'm struggling with. So far I have the following:
((?<="body"\s:\s").*(?=",))
Every guide I look at, though, doesn't match the commas within the quotes. Any suggestions?
You can use
(\G(?!^)|\"body\"\s*:\s*\")([^\",]*),
In case there are escape sequences in the string use
(\G(?!^)|\"body\"\s*:\s*\")([^\",\\]*(?:\\.[^\",\\]*)*),
See the regex demo (and regex demo #2), replace with $1$2.
Details:
(\G(?!^)|\"body\"\s*:\s*\") - Group 1: end of the previous match or "body", zero or more whitespaces, :, zero or more whitespaces
([^\",]*) - Group 2 ($2): any zero or more chars other than " and ,
, - a comma (to be removed/replaced).

Is there a Google Sheets or MS Excel formula to convert a column with JSON array to multiple rows

I'm trying to parse a column with JSON array data in an Excel file. I want to convert that JSON array to multiple rows.
I've tried using split text to columns, but that is giving inconsistent results.
Input:
Expected Output:
You can find my input and expected_output sheet in this Google sheet
https://docs.google.com/spreadsheets/d/1zohaZXEoppbn4GBq-BLEFHk5n4PPwYPOTnbZbq6YbNs/edit?usp=sharing
there are many ways one of which is:
=ARRAYFORMULA(SPLIT(TRANSPOSE(SPLIT(TRIM(QUERY(TRANSPOSE(QUERY(TRANSPOSE(
IF(IFERROR(SPLIT(C2:C, ","))<>"", "♦"&A2:A&"♠"&B2:B&"♠"&
REGEXEXTRACT(SPLIT(SUBSTITUTE(C2:C, """", ), ","), ":(\d+)"), ))
,,999^99)),,999^99)), "♦")), "♠"))
=ARRAYFORMULA(SPLIT(TRANSPOSE(SPLIT(TRIM(QUERY(TRANSPOSE(QUERY(TRANSPOSE(
IF(LEN(A2), "♦"&A2&"♠"&B2&QUERY(TRANSPOSE("♠"&QUERY(REGEXEXTRACT(SPLIT(
TRANSPOSE(SPLIT(SUBSTITUTE(C2, """", ), "{")), ","), ":(\d+)"), "offset 1", 0))
,,999^99), )),,999^99)),,999^99)), "♦")), "♠"))
One may adopt the code from my project.
json is a tricky format, it uses different data types and also may contain nested items:
{ key: { key1: "a", key2: "b" } }
This is the reason to use google-apps-script for the task.
Please, copy the sample file:
or see the source code here:
https://github.com/Max-Makhrov/GoogleSheets/blob/master/Json.gs
Sample usage:
=getJsonArrayAsTable(C2)
I needed to do something similar today in Google Sheets. The solution I came up with is surprisingle simple. It may be useful but has a couple of dependencies:
The JSON data conforms to a Google Apps Script Range-like structure (examples below)**.
A custom function defined using Google Apps Script.
The custom function
function JSONTOJS(JSONString) {
return JSON.parse(JSONString);
}
Sample Data
A simple list:
["Option 1", "Option 2", "Option 3", "Option 4"]
Tabular data:
[["Title 1", "Title 2", "Title 3", "Title 4"], ["Option 1", "Option 2", "Option 3", "Option 4"], ["Option 5", "Option 6", "Option 7", "Option 8"]]
Key:value pairs:
[["key1","value 1"], ["key2","value 2"], ["key3","value 3"]]
In use:
Assuming one of these JSON strings was in cell A1, in some other cell input the following formula and the data will be displayed in the cells in your Sheet.
=JSONTOJS(A1)
Example Sheet (get a copy of it)
** If you're defining the data structure, this may be useful. You're unlikely to receive data from some service in this format (it is quite Google Sheets unique), in which case your mileage will vary.
google-apps-script

Notepad++: What is the "opposite" format of JSFormat?

I'm looking for the "opposite" Format of JSFormat from the JSTools. Here an example:
JSON code example:
title = Automatic at 07.02.17 & appId = ID_1 & data = {
"base": "+:background1,background2",
"content": [{
"appTitle": "Soil",
"service": {
"serviceType": "AG",
"Url": "http://test.de/xxx"
},
"opacity": "1"]
}
],
"center": "4544320.372869264,5469450.086030475,31468"
}
& context = PARAMETERS
and I Need to convert the Format to the following format:
title=Automatic at 07.02.17 &appId=ID_1&data={"base":"+:background1,background2","content":[{"appTitle":"Soil","service":{"serviceType":"AG","Url":"http://test.de/xxx"},"opacity":"1"]}],"center":"4544320.372869264,5469450.086030475,31468"}&context=PARAMETERS
which is a decoded URL (with MIME Tools) from this html POST:
title%3DAutomatic%20at%2007.02.17%20%26appId%3DID_1%26data%3D%7B%22base%22%3A%22+%3Abackground1,background2%22,%22content%22%3A%5B%7B%22appTitle%22%3A%22Soil%22,%22service%22%3A%7B%22serviceType%22%3A%22AG%22,%22Url%22%3A%22http%3A%2F%2Ftest.de%2Fxxx%22%7D,%22opacity%22%3A%221%22%5D%7D%5D,%22center%22%3A%224544320.372869264,5469450.086030475,31468%22%7D%26context%3DPARAMETERS%0D%0A
which I have to come back after doing changes in the JSON code. From the second to the third Format I can use URL encode (MIME Tools), but what about the reformating from the first to the second Format.
My question: Do you have ideas how to turn the first (JSON) Format into the second (decoded URL) in Notepad++? Something like the "opposite" of JSFormat?
If I understand correctly you basically need to put your JSON on a single line removing new lines and spaces.
This should be achieved with these steps:
CTRL + H to replace occurrences of more than one space with empty string using this regex: [ ]{2,} (remember to select "Regular expression" radiobutton). If this is not exactly what you want you can adjust the regular expression to achieve desired output
select all your JSON CTRL + A
put everything on a single line with join CTRL + J
You can also record a macro to automate this process and run it with a keyboard shortcut.

Import specific json value in google spreadsheets

I'm trying to import some facebook data into spreadsheets.
The request
https://graph.facebook.com/v2.6/{user id}?fields=name&access_token=1{my token}
Gets me
{
"name": "user",
"id": "numeric id"
}
When I import it to spreadsheets with:
=IMPORTDATA(https://graph.facebook.com/v2.6/{user id}?access_token={my token}
)
On the cells, I get {"name": "user" in one cell and "id": "numeric id" in another. But I only need the "name" value, How can I target just that value, in only one cell as I have no use for the second value.
Since you want only one cell from the imported range, which happens to be its upper left corner, the array_constrain function can help:
=array_constrain(importdata("..."), 1, 1)
Generally, the 2nd and 3rd arguments of array_constrain determine how many rows and how many columns you want to keep.
You can also get rid of "name": at the beginning by using the text processing functions such as left, right, regexreplace, etc. For example, wrapping the previous formula in
=regexreplace( ... , "^\S+: |""", "")
will get rid of "name": and of the quote marks around the name.