Affichage des articles dont le libellé est json. Afficher tous les articles
Affichage des articles dont le libellé est json. Afficher tous les articles

27 oct. 2016

[JSON] Diviser (ou éclater) un fichier avec jq

Je souhaitais éclater un fichier json en plein de petits fichiers.

pour ce faire, il faut installer jq
$ sudo yum install -y jq
j'ai fais ensuite un script (json_spliter.sh):
#!/bin/sh
file=$1
output_dir="splited"
count=1
mkdir $output_dir
cat $file | jq -c -M '.[]' | sed 's/\\"/\\\\"/g' | \
while read line
do
        echo $line > $output_dir/$count.json
        count=`expr $count + 1`
done
Il suffit pour l'utiliser :
$ ./json_spliter.sh fichier.json

[JSON] Convertir un csv en json

on part du principe que l'on est sous linux.
on a installé nodejs avec npm

Tapper :
$ sudo npm install -g csv2json
pour l'utiliser:
$ csv2json fichier.csv
(ce qui va afficher le json à la volée)

on peut donc rediriger le tout vers un fichier:
$ csv2json fichier.csv > fichier.json


26 avr. 2016

[JSON] correcteur de syntaxe

Comment corriger sa syntaxe ou détecter des erreurs?

il peut arriver que l'on oublie ou laisse une virgule là où il ne faut pas. Qu'une accolade ne soit pas fermée...

Ce site est pas mal pour tester son code json:

https://jsonformatter.curiousconcept.com/

24 févr. 2016

[NodeJS] Comment déployer node.js (en)

Quelques vidéos en anglais sur le déploiement de node.js

SpinUp a machine



Deploy the app


18 févr. 2016

[JAVASCRIPT] XML <->JSON (xml2json)

L'outils XML2JSON permet de convertir des fichier XML en JSON ce qui peut être pratique dans
certains cas de figures.
English Title: "How to convert  XML to JSON and JSON to XML in CLI"

les sources de xml2json https://www.npmjs.com/package/xml2json

Installation

$ npm install xml2json
npm http GET https://registry.npmjs.org/xml2json
npm http 200 https://registry.npmjs.org/xml2json
npm http GET https://registry.npmjs.org/xml2json/-/xml2json-0.9.0.tgz
npm http 200 https://registry.npmjs.org/xml2json/-/xml2json-0.9.0.tgz
[...]

Un répertoire nommé "node_modules" se créé là où vous avez exécuter la commande

Utiliser xml2json pour transformer du xml en json

Tout d'abord localisons xml2json: Il se trouve dans node_modules/bin et taper la commande sous la forme suivante:

$ cat monfichier.xml  | /home/stan/node_modules/xml2json/bin/xml2json > monfichier.json

Le fichier JSON est alors généré

On peut alors utiliser jq (https://stedolan.github.io/jq/tutorial/) pour bien formater le JSON.

Utiliser xml2json pour transformer du json en xml

Là ça se complique ! il y a bien un json2xml, mais pas de binaire pour l’exécuter, juste des libs
Je les ai quand même installées:

Installation
# npm install xml2json
Ensuite aller dans ~/node_modules/xml2json/bin/ (oui je dis bien dans xml2json et pas json2xml)

et créer le fichier json2xml

$ vim json2xml
Contenu du fichier:
#!/usr/bin/env node

var json2xml = require('../');
var pkg = require('../package.json');

var json = '';

var args = process.argv.slice(2)
var arg = args[0]

if (arg == '--version') {
        console.log(pkg.version)
        process.exit(0)
}

process.stdin.on('data', function (data) {
        json += data;
});

process.stdin.resume();

process.stdin.on('end', function () {
        xml = json2xml.toXml(json)
        process.stdout.write(xml + '\n')
});
Enregistrer et quitter, puis rendez le exécutable:

$ chmod +x json2xml
puis l'executer:
$ cat $JSONFILE | /opt/tools/node_modules/xml2json/bin/json2xml > $XMLFILE

 Et voilà

17 févr. 2016

[JSON] Comment parser du JSON via Linux (EN)

How to parse JSON string via command line on Linux

If you often deal with JSON-formatted texts from the command line or in shell scripts, you may wonder if there is any command-line utility which can parse JSON string. A command-line JSON parser can be handy when you test or debug JSON web services. You can feed JSON-formatted responses from web services into the command-line JSON parser, thereby easily inspecting otherwise hard-to-read JSON responses or extracting individual objects from them.

In this tutorial, I will describe how to parse JSON string from the command line.

On Linux, there is a command-line JSON processor called jq which does exactly that. Using jq, you can parse, filter, map, and transform JSON-structured data effortlessly.

To install jq on Linux, simply download its binary (available for 32-bit and 64-bit system separately) as follows.
$ wget http://stedolan.github.io/jq/download/linux32/jq (32-bit system)
$ wget http://stedolan.github.io/jq/download/linux64/jq (64-bit system)
$ chmod +x ./jq
$ sudo cp jq /usr/bin
The jq binary is also available for Windows and OS X platforms, and its full source code is released under the MIT license.

The following examples illustrate how to parse JSON-structured data with jq.

An example JSON Schema:
$ cat json.txt

{
        "name": "Google",
        "location":
                {
                        "street": "1600 Amphitheatre Parkway",
                        "city": "Mountain View",
                        "state": "California",
                        "country": "US"
                },
        "employees":
                [
                        {
                                "name": "Michael",
                                "division": "Engineering"
                        },
                        {
                                "name": "Laura",
                                "division": "HR"
                        },
                        {
                                "name": "Elise",
                                "division": "Marketing"
                        }
                ]
}
To parse a JSON object:
$ cat json.txt | jq '.name'

"Google"
To parse a nested JSON object:
$ cat json.txt | jq '.location.city'

"Mountain View"
To parse a JSON array:
$ cat json.txt | jq '.employees[0].name'

"Michael"
To extract specific fields from a JSON object:
$ cat json.txt | jq '.location | {street, city}'

{
  "city": "Mountain View",
  "street": "1600 Amphitheatre Parkway"
}


Différences majeures entre Red Hat 6, 7, 8 et 9

Quelles sont les différences majeures entre RHEL 6, 7, 8 et 9 ? Système de fichiers RHEL 6: Par défaut : ext4. Autres : ext2, ext3 supportés...